1010@description: 简单的窗口贴边隐藏
1111"""
1212
13+ import os
14+ import platform
15+ from subprocess import getoutput
16+
1317try :
1418 from PyQt5 .QtCore import Qt
15- from PyQt5 .QtWidgets import QWidget , QVBoxLayout , QPushButton , QApplication
19+ from PyQt5 .QtWidgets import (
20+ QApplication ,
21+ QGridLayout ,
22+ QMessageBox ,
23+ QPushButton ,
24+ QSizePolicy ,
25+ QSpacerItem ,
26+ QWidget ,
27+ )
1628except ImportError :
1729 from PySide2 .QtCore import Qt
18- from PySide2 .QtWidgets import QWidget , QVBoxLayout , QPushButton , QApplication
30+ from PySide2 .QtWidgets import (
31+ QApplication ,
32+ QGridLayout ,
33+ QMessageBox ,
34+ QPushButton ,
35+ QSizePolicy ,
36+ QSpacerItem ,
37+ QWidget ,
38+ )
1939
2040
21- class WeltHideWindow (QWidget ):
41+ def IsSupport ():
42+ """判断是否支持"""
43+ if platform .system () == "Linux" :
44+ name = os .environ .get ("XDG_SESSION_DESKTOP" , "" ) + os .environ .get (
45+ "XDG_CURRENT_DESKTOP" , ""
46+ )
47+ if name .lower ().find ("gnome" ) != - 1 :
48+ print ("gnome desktop" )
49+ return False
50+
51+ wid = getoutput ("xprop -root _NET_SUPPORTING_WM_CHECK" ).split (" # " )[- 1 ]
52+ print ("wid:" , wid )
53+ if wid :
54+ name = getoutput ("xprop -id %s _NET_WM_NAME" % wid )
55+ print ("name:" , name )
56+ if name .lower ().find ("gnome" ) != - 1 :
57+ print ("gnome desktop" )
58+ return False
2259
60+ return True
61+
62+
63+ class WeltHideWindow (QWidget ):
2364 def __init__ (self , * args , ** kwargs ):
2465 super (WeltHideWindow , self ).__init__ (* args , ** kwargs )
2566 self .setWindowFlags (self .windowFlags () | Qt .FramelessWindowHint )
26- self .resize (800 , 600 )
67+ self .resize (400 , 300 )
2768 self ._width = QApplication .desktop ().availableGeometry (self ).width ()
28- layout = QVBoxLayout (self )
29- layout .addWidget (QPushButton ("关闭窗口" , self , clicked = self .close ))
69+ layout = QGridLayout (self )
70+ layout .addItem (
71+ QSpacerItem (40 , 20 , QSizePolicy .Expanding , QSizePolicy .Minimum ), 0 , 0
72+ )
73+ self .closeBtn = QPushButton ("X" , self )
74+ layout .addWidget (self .closeBtn , 0 , 1 )
75+ layout .addItem (
76+ QSpacerItem (20 , 40 , QSizePolicy .Minimum , QSizePolicy .Expanding ), 1 , 0
77+ )
78+ self .closeBtn .clicked .connect (self .close )
79+ self .closeBtn .setMinimumSize (24 , 24 )
80+ self .closeBtn .setMaximumSize (24 , 24 )
3081
3182 def mousePressEvent (self , event ):
32- ''' 鼠标按下事件,需要记录下坐标self._pos 和 是否可移动self._canMove'''
83+ """ 鼠标按下事件,需要记录下坐标self._pos 和 是否可移动self._canMove"""
3384 super (WeltHideWindow , self ).mousePressEvent (event )
3485 if event .button () == Qt .LeftButton :
3586 self ._pos = event .globalPos () - self .pos ()
3687 # 当窗口最大化或者全屏时不可移动
3788 self ._canMove = not self .isMaximized () or not self .isFullScreen ()
3889
3990 def mouseMoveEvent (self , event ):
40- ''' 鼠标移动事件,动态调整窗口位置'''
91+ """ 鼠标移动事件,动态调整窗口位置"""
4192 super (WeltHideWindow , self ).mouseMoveEvent (event )
4293 if event .buttons () == Qt .LeftButton and self ._canMove :
4394 self .move (event .globalPos () - self ._pos )
4495
4596 def mouseReleaseEvent (self , event ):
46- ''' 鼠标弹起事件,这个时候需要判断窗口的左边是否符合贴到左边,顶部,右边一半'''
97+ """ 鼠标弹起事件,这个时候需要判断窗口的左边是否符合贴到左边,顶部,右边一半"""
4798 super (WeltHideWindow , self ).mouseReleaseEvent (event )
4899 self ._canMove = False
49100 pos = self .pos ()
@@ -60,7 +111,7 @@ def mouseReleaseEvent(self, event):
60111 return self .move (self ._width - 1 , y )
61112
62113 def enterEvent (self , event ):
63- ''' 鼠标进入窗口事件,用于弹出显示窗口'''
114+ """ 鼠标进入窗口事件,用于弹出显示窗口"""
64115 super (WeltHideWindow , self ).enterEvent (event )
65116 pos = self .pos ()
66117 x = pos .x ()
@@ -73,7 +124,7 @@ def enterEvent(self, event):
73124 return self .move (self ._width - self .width (), y )
74125
75126 def leaveEvent (self , event ):
76- ''' 鼠标离开事件,如果原先窗口已经隐藏,并暂时显示,此时离开后需要再次隐藏'''
127+ """ 鼠标离开事件,如果原先窗口已经隐藏,并暂时显示,此时离开后需要再次隐藏"""
77128 super (WeltHideWindow , self ).leaveEvent (event )
78129 pos = self .pos ()
79130 x = pos .x ()
@@ -86,10 +137,18 @@ def leaveEvent(self, event):
86137 return self .move (self ._width - 1 , y )
87138
88139
89- if __name__ == ' __main__' :
140+ if __name__ == " __main__" :
90141 import sys
91142
92143 app = QApplication (sys .argv )
93- w = WeltHideWindow ()
94- w .show ()
95- sys .exit (app .exec_ ())
144+ if not IsSupport ():
145+ QMessageBox .warning (
146+ None ,
147+ "Warning" ,
148+ "当前桌面不支持此功能" ,
149+ )
150+ app .quit ()
151+ else :
152+ w = WeltHideWindow ()
153+ w .show ()
154+ sys .exit (app .exec_ ())
0 commit comments