10
10
@description: 简单的窗口贴边隐藏
11
11
"""
12
12
13
+ import os
14
+ import platform
15
+ from subprocess import getoutput
16
+
13
17
try :
14
18
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
+ )
16
28
except ImportError :
17
29
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
+ )
19
39
20
40
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
22
59
60
+ return True
61
+
62
+
63
+ class WeltHideWindow (QWidget ):
23
64
def __init__ (self , * args , ** kwargs ):
24
65
super (WeltHideWindow , self ).__init__ (* args , ** kwargs )
25
66
self .setWindowFlags (self .windowFlags () | Qt .FramelessWindowHint )
26
- self .resize (800 , 600 )
67
+ self .resize (400 , 300 )
27
68
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 )
30
81
31
82
def mousePressEvent (self , event ):
32
- ''' 鼠标按下事件,需要记录下坐标self._pos 和 是否可移动self._canMove'''
83
+ """ 鼠标按下事件,需要记录下坐标self._pos 和 是否可移动self._canMove"""
33
84
super (WeltHideWindow , self ).mousePressEvent (event )
34
85
if event .button () == Qt .LeftButton :
35
86
self ._pos = event .globalPos () - self .pos ()
36
87
# 当窗口最大化或者全屏时不可移动
37
88
self ._canMove = not self .isMaximized () or not self .isFullScreen ()
38
89
39
90
def mouseMoveEvent (self , event ):
40
- ''' 鼠标移动事件,动态调整窗口位置'''
91
+ """ 鼠标移动事件,动态调整窗口位置"""
41
92
super (WeltHideWindow , self ).mouseMoveEvent (event )
42
93
if event .buttons () == Qt .LeftButton and self ._canMove :
43
94
self .move (event .globalPos () - self ._pos )
44
95
45
96
def mouseReleaseEvent (self , event ):
46
- ''' 鼠标弹起事件,这个时候需要判断窗口的左边是否符合贴到左边,顶部,右边一半'''
97
+ """ 鼠标弹起事件,这个时候需要判断窗口的左边是否符合贴到左边,顶部,右边一半"""
47
98
super (WeltHideWindow , self ).mouseReleaseEvent (event )
48
99
self ._canMove = False
49
100
pos = self .pos ()
@@ -60,7 +111,7 @@ def mouseReleaseEvent(self, event):
60
111
return self .move (self ._width - 1 , y )
61
112
62
113
def enterEvent (self , event ):
63
- ''' 鼠标进入窗口事件,用于弹出显示窗口'''
114
+ """ 鼠标进入窗口事件,用于弹出显示窗口"""
64
115
super (WeltHideWindow , self ).enterEvent (event )
65
116
pos = self .pos ()
66
117
x = pos .x ()
@@ -73,7 +124,7 @@ def enterEvent(self, event):
73
124
return self .move (self ._width - self .width (), y )
74
125
75
126
def leaveEvent (self , event ):
76
- ''' 鼠标离开事件,如果原先窗口已经隐藏,并暂时显示,此时离开后需要再次隐藏'''
127
+ """ 鼠标离开事件,如果原先窗口已经隐藏,并暂时显示,此时离开后需要再次隐藏"""
77
128
super (WeltHideWindow , self ).leaveEvent (event )
78
129
pos = self .pos ()
79
130
x = pos .x ()
@@ -86,10 +137,18 @@ def leaveEvent(self, event):
86
137
return self .move (self ._width - 1 , y )
87
138
88
139
89
- if __name__ == ' __main__' :
140
+ if __name__ == " __main__" :
90
141
import sys
91
142
92
143
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