如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# -*- coding: utf-8 -*- import sys import os from pyqt5 import qtcore, qtgui, qtwidgets from pyqt5 import qtcore, qtgui, qtwidgets from pyqt5.qtcore import qrect, qsize, qmetaobject, qcoreapplication,\ qpropertyanimation from pyqt5.qtgui import qfont from pyqt5.qtwidgets import qfiledialog from pyqt5.qtwidgets import qwidget, qhboxlayout, qgridlayout, qpushbutton,\ qapplication, qmainwindow class ui_mainwindow( object ): def setupui( self , mainwindow): mainwindow.setobjectname( "mainwindow" ) mainwindow.resize( 800 , 681 ) mainwindow.setminimumsize(qtcore.qsize( 0 , 651 )) mainwindow.setstylesheet( "background-image: url(:/新前綴/backpic.png);" ) self .centralwidget = qtwidgets.qwidget(mainwindow) self .centralwidget.setobjectname( "centralwidget" ) self .widget = qtwidgets.qwidget( self .centralwidget) self .widget.setgeometry(qtcore.qrect( 0 , - 580 , 195 , 631 )) self .widget.setobjectname( "widget" ) self .scrollarea = qtwidgets.qscrollarea( self .widget) self .scrollarea.setgeometry(qtcore.qrect( 0 , 0 , 195 , 581 )) self .scrollarea.setwidgetresizable(true) self .scrollarea.setobjectname( "scrollarea" ) self .scrollareawidgetcontents = qtwidgets.qwidget() self .scrollareawidgetcontents.setgeometry(qtcore.qrect( 0 , 0 , 178 , 681 )) self .scrollareawidgetcontents.setminimumsize(qtcore.qsize( 177 , 681 )) self .scrollareawidgetcontents.setobjectname( "scrollareawidgetcontents" ) self .verticallayoutwidget = qtwidgets.qwidget( self .scrollareawidgetcontents) self .verticallayoutwidget.setgeometry(qtcore.qrect( 0 , - 10 , 181 , 691 )) self .verticallayoutwidget.setobjectname( "verticallayoutwidget" ) self .verticallayout = qtwidgets.qvboxlayout( self .verticallayoutwidget) self .verticallayout.setcontentsmargins( 0 , 0 , 0 , 0 ) self .verticallayout.setspacing( 20 ) self .verticallayout.setobjectname( "verticallayout" ) self .pushbutton_2 = qtwidgets.qpushbutton( self .verticallayoutwidget) self .pushbutton_2.setobjectname( "pushbutton_2" ) self .verticallayout.addwidget( self .pushbutton_2) self .label_2 = qtwidgets.qlabel( self .verticallayoutwidget) self .label_2.setobjectname( "label_2" ) self .verticallayout.addwidget( self .label_2) self .pushbutton_4 = qtwidgets.qpushbutton( self .verticallayoutwidget) self .pushbutton_4.setobjectname( "pushbutton_4" ) self .verticallayout.addwidget( self .pushbutton_4) self .label_3 = qtwidgets.qlabel( self .verticallayoutwidget) self .label_3.setobjectname( "label_3" ) self .verticallayout.addwidget( self .label_3) self .pushbutton_7 = qtwidgets.qpushbutton( self .verticallayoutwidget) self .pushbutton_7.setobjectname( "pushbutton_7" ) self .verticallayout.addwidget( self .pushbutton_7) self .label_4 = qtwidgets.qlabel( self .verticallayoutwidget) self .label_4.setobjectname( "label_4" ) self .verticallayout.addwidget( self .label_4) self .pushbutton_6 = qtwidgets.qpushbutton( self .verticallayoutwidget) self .pushbutton_6.setobjectname( "pushbutton_6" ) self .verticallayout.addwidget( self .pushbutton_6) self .label_5 = qtwidgets.qlabel( self .verticallayoutwidget) self .label_5.setobjectname( "label_5" ) self .verticallayout.addwidget( self .label_5) self .pushbutton_5 = qtwidgets.qpushbutton( self .verticallayoutwidget) self .pushbutton_5.setobjectname( "pushbutton_5" ) self .verticallayout.addwidget( self .pushbutton_5) self .label_6 = qtwidgets.qlabel( self .verticallayoutwidget) self .label_6.setobjectname( "label_6" ) self .verticallayout.addwidget( self .label_6) self .pushbutton = qtwidgets.qpushbutton( self .verticallayoutwidget) self .pushbutton.setobjectname( "pushbutton" ) self .verticallayout.addwidget( self .pushbutton) self .label_7 = qtwidgets.qlabel( self .verticallayoutwidget) self .label_7.setobjectname( "label_7" ) self .verticallayout.addwidget( self .label_7) self .scrollarea.setwidget( self .scrollareawidgetcontents) self .closebutton = qtwidgets.qpushbutton( self .widget) self .closebutton.setgeometry(qtcore.qrect( 50 , 580 , 61 , 32 )) self .closebutton.setstylesheet( "border-image: url(:/新前綴/v.png);" ) self .closebutton.settext("") self .closebutton.setobjectname( "closebutton" ) self .openbutton = qtwidgets.qpushbutton( self .centralwidget) self .openbutton.setgeometry(qtcore.qrect( 50 , 0 , 61 , 32 )) self .openbutton.setstylesheet( "border-image: url(:/新前綴/xia.png);" ) self .openbutton.settext("") self .openbutton.setobjectname( "openbutton" ) self .graphicsview = qtwidgets.qgraphicsview( self .centralwidget) self .graphicsview.setgeometry(qtcore.qrect( 0 , 0 , 801 , 641 )) self .graphicsview.setobjectname( "graphicsview" ) self .widget.raise_() self .graphicsview.raise_() self .openbutton.raise_() mainwindow.setcentralwidget( self .centralwidget) self .menubar = qtwidgets.qmenubar(mainwindow) self .menubar.setgeometry(qtcore.qrect( 0 , 0 , 800 , 22 )) self .menubar.setobjectname( "menubar" ) self .menu = qtwidgets.qmenu( self .menubar) self .menu.setobjectname( "menu" ) mainwindow.setmenubar( self .menubar) self .statusbar = qtwidgets.qstatusbar(mainwindow) self .statusbar.setobjectname( "statusbar" ) mainwindow.setstatusbar( self .statusbar) self .action = qtwidgets.qaction(mainwindow) self .action.setobjectname( "action" ) self .action_2 = qtwidgets.qaction(mainwindow) self .action_2.setobjectname( "action_2" ) self .action_3 = qtwidgets.qaction(mainwindow) self .action_3.setobjectname( "action_3" ) self .menu.addaction( self .action) self .menu.addaction( self .action_2) self .menu.addaction( self .action_3) self .menubar.addaction( self .menu.menuaction()) self .retranslateui(mainwindow) qtcore.qmetaobject.connectslotsbyname(mainwindow) def retranslateui( self , mainwindow): _translate = qtcore.qcoreapplication.translate mainwindow.setwindowtitle(_translate( "mainwindow" , "mainwindow" )) self .pushbutton_2.settext(_translate( "mainwindow" , "pushbutton" )) self .label_2.settext(_translate( "mainwindow" , "textlabel" )) self .pushbutton_4.settext(_translate( "mainwindow" , "pushbutton" )) self .label_3.settext(_translate( "mainwindow" , "textlabel" )) self .pushbutton_7.settext(_translate( "mainwindow" , "pushbutton" )) self .label_4.settext(_translate( "mainwindow" , "textlabel" )) self .pushbutton_6.settext(_translate( "mainwindow" , "pushbutton" )) self .label_5.settext(_translate( "mainwindow" , "textlabel" )) self .pushbutton_5.settext(_translate( "mainwindow" , "pushbutton" )) self .label_6.settext(_translate( "mainwindow" , "textlabel" )) self .pushbutton.settext(_translate( "mainwindow" , "pushbutton" )) self .label_7.settext(_translate( "mainwindow" , "textlabel" )) self .menu.settitle(_translate( "mainwindow" , "人腦架構分析" )) self .action.settext(_translate( "mainwindow" , "導入矩陣" )) self .action_2.settext(_translate( "mainwindow" , "退出系統" )) self .action_3.settext(_translate( "mainwindow" , "開發者信息" )) class window(qmainwindow, ui_mainwindow): def __init__( self , * args, * * kwargs): super (window, self ).__init__( * args, * * kwargs) self .setupui( self ) self .outani = qpropertyanimation( self .widget, b "geometry" ) # 背后隱藏的控件 self .outani.setduration( 1000 ) # 1s self .outani.setendvalue(qrect( 0 , 30 , 178 , 681 )) # 只是x坐標變化 # 移進去動畫 self .inani = qpropertyanimation( self .widget, b "geometry" ) # 背后隱藏的控件 self .inani.setduration( 1000 ) # 1s self .inani.setendvalue(qrect( 0 , - 580 , 178 , 681 )) self .inani.finished.connect( self .onfinish) # 綁定打開關閉事件 self .openbutton.clicked.connect( self .onopen) self .closebutton.clicked.connect( self .onclose) #打開文件 self .action.triggered.connect( self .openfile) self .action.setstatustip( '導入矩陣' ) self .action.setshortcut( 'ctrl+q' ) #退出系統 self .action_2.triggered.connect( self .close) self .action_2.setstatustip( '退出系統' ) self .action_2.setshortcut( 'ctrl+e' ) #開發者信息 self .action_3.setstatustip( '唐偉澤 謝家柯 2016.12.3' ) def onopen( self ): # 設置按鈕不可見 self .openbutton.setvisible(false) self .widget.setvisible(true) # 背后的待拉出來的控件可見 self .graphicsview.setgeometry(qtcore.qrect( 195 , 1 , 611 , 641 )) self .outani.start() # 開啟動畫效果 def onclose( self ): self .outani.stop() # 如果移動到一半則停止 self .inani.start() # 退回去 def onfinish( self ): # 關閉動畫結束 self .widget.setvisible(false) self .openbutton.setvisible(true) self .graphicsview.setgeometry(qtcore.qrect( 0 , 0 , 801 , 641 )) def openfile( self ): filename1, filetype = qfiledialog.getopenfilename( self , "選取文件" , "/users/kelisiya/desktop" , "all files (*);;text files (*.txt)" ) print (filename1, filetype) if __name__ = = "__main__" : import sys app = qapplication(sys.argv) w = window() w.show() sys.exit(app.exec_()) |
以上這篇pyqt5 實現 下拉菜單 + 打開文件的示例代碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_33638791/article/details/53467643