1. 用qt designer编写主窗体,窗体类型是mainwindow,空白窗口上一个按钮。并转换成mainwindow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# -*- coding: utf-8 -*-
# form implementation generated from reading ui file 'f.ui'
#
# created by: pyqt5 ui code generator 5.9
#
# warning! all changes made in this file will be lost!
from pyqt5 import qtcore, qtgui, qtwidgets
class ui_mainwindow( object ):
def setupui( self , mainwindow):
mainwindow.setobjectname( "mainwindow" )
mainwindow.resize( 800 , 600 )
self .centralwidget = qtwidgets.qwidget(mainwindow)
self .centralwidget.setobjectname( "centralwidget" )
self .pushbutton = qtwidgets.qpushbutton( self .centralwidget)
self .pushbutton.setgeometry(qtcore.qrect( 80 , 90 , 75 , 23 ))
self .pushbutton.setobjectname( "pushbutton" )
mainwindow.setcentralwidget( self .centralwidget)
self .retranslateui(mainwindow)
qtcore.qmetaobject.connectslotsbyname(mainwindow)
def retranslateui( self , mainwindow):
_translate = qtcore.qcoreapplication.translate
mainwindow.setwindowtitle(_translate( "mainwindow" , "mainwindow" ))
self .pushbutton.settext(_translate( "mainwindow" , "pushbutton" ))
|
2. 用qt designer编写子窗体,窗体类型是dialog, 空白窗口上一个按钮。并转换成childwindow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from pyqt5 import qtcore, qtgui, qtwidgets
class ui_dialog( object ):
def setupui( self , dialog):
dialog.setobjectname( "dialog" )
dialog.resize( 400 , 300 )
self .pushbutton = qtwidgets.qpushbutton(dialog)
self .pushbutton.setgeometry(qtcore.qrect( 160 , 100 , 75 , 23 ))
self .pushbutton.setobjectname( "pushbutton" )
dialog.setwindowflags(qtcore.qt.windowstaysontophint) #设置窗体总显示在最上面
self .retranslateui(dialog)
qtcore.qmetaobject.connectslotsbyname(dialog)
def retranslateui( self , dialog):
_translate = qtcore.qcoreapplication.translate
dialog.setwindowtitle(_translate( "dialog" , "dialog" ))
self .pushbutton.settext(_translate( "dialog" , "pushbutton" ))
|
3,编写调用程序,这个重点,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import sys
from pyqt5.qtwidgets import qapplication, qmainwindow, qdialog
from dust.mainwindow import *
from dust.childwindow import *
if __name__ = = '__main__' :
app = qapplication(sys.argv)
#实例化主窗口
main = qmainwindow()
main_ui = ui_mainwindow()
main_ui.setupui(main )
#实例化子窗口
child = qdialog()
child_ui = ui_dialog()
child_ui.setupui(child)
#按钮绑定事件
btn = main_ui.pushbutton
btn.clicked.connect( child.show )
#显示
main.show()
sys.exit(app.exec_())
|
4. 上面的程序只是能显示了,要想添加自定义事件,还不行,加自定义事件,有一个办法是再封装一个类,主窗体和子窗体都如此。
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
|
import sys
from pyqt5.qtwidgets import qapplication, qmainwindow, qdialog
from dust.mainwindow import *
from dust.childwindow import *
#mainwindow
class mymainwindow(qmainwindow, ui_mainwindow):
def __init__( self ):
super (mymainwindow, self ).__init__()
self .setupui( self )
self .setgeometry( 0 , 0 , 1024 , 600 )
self .setwindowtitle( 'main window' )
def paintevent( self , event):
painter = qpainter( self )
pixmap = qpixmap( "./image/bg.jpg" )
painter.drawpixmap( self .rect(),pixmap)
def keypressevent( self , e):
if e.key() = = qt.key_escape:
self .close()
class childwindow(qdialog, ui_dialog):
def __init__( self ):
super (childwindow, self ).__init__()
self .setupui( self )
self .setwindowtitle( 'child window' )
self .pushbutton.clicked.connect( self .btnclick) #按钮事件绑定
def btnclick( self ): #子窗体自定义事件
self .close()
if __name__ = = '__main__' :
app = qapplication(sys.argv)
main = mymainwindow()
child = childwindow()
btn = main.pushbutton #主窗体按钮事件绑定
btn.clicked.connect( child.show )
main.show()
sys.exit(app.exec_())
|
以上这篇pyqt5对用qt designer设计的窗体实现弹出子窗口的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/wzxxtt62267018/article/details/80897185