pyqt5使用按钮进行界面的跳转方法

时间:2021-10-02 04:18:44

简介

进行按钮进行界面的跳转,我这里面我介绍两种,一种是没有使用qtdesigner的代码,另一种是使用qtdesigner的代码

代码1

?
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
import sys
from pyqt5.qtwidgets import qmainwindow, qpushbutton, qapplication
 
 
class first(qmainwindow):
 
  def __init__(self):
    super().__init__()
 
    self.initui()
 
  def initui(self):
    self.btn = qpushbutton("button", self)
    self.btn.move(30, 50)
    
 
 
    self.setgeometry(300, 300, 500, 500)
    self.setwindowtitle('event sender')
    self.show()
 
class second(qmainwindow):
  def __init__(self):
    super().__init__()
 
    self.initui()
 
  def initui(self):
    self.setgeometry(300, 300, 290, 150)
    self.setwindowtitle('get sender')
 
 
if __name__ == '__main__':
  app = qapplication(sys.argv)
  a = first()
  b = second()
  a.show()
  a.btn.clicked.connect(b.show)
  sys.exit(app.exec_())

代码2

下面的代码使用了qtdesigner的代码

?
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
from pyqt5 import qtcore, qtgui, qtwidgets
import sys
from pyqt5.qtwidgets import qapplication, qmainwindow, qwidget, qlabel,\
                     qpushbutton, qlineedit, qmenubar, qstatusbar
from pyqt5.qtcore import *
 
class firstwindow(qwidget):
 
  def __init__(self, parent=none):
    # super这个用法是调用父类的构造函数
    # parent=none表示默认没有父widget,如果指定父亲widget,则调用之
    super(firstwindow, self).__init__(parent)
    self.setgeometry(500, 500, 500, 500)
    self.setwindowtitle('显示')
 
    self.btn = qpushbutton(self)
    self.btn.settext('标定')
    self.btn.move(150, 50)
 
 
#下面的一部分是qtdesigner的代码
 
class ui_mainwindow(object):
  def setupui(self, mainwindow):
    mainwindow.setobjectname("mainwindow")
    mainwindow.resize(624, 479)
    self.centralwidget = qtwidgets.qwidget(mainwindow)
    self.centralwidget.setobjectname("centralwidget")
    self.label = qtwidgets.qlabel(self.centralwidget)
    self.label.setgeometry(qtcore.qrect(190, 130, 51, 31))
    self.label.setobjectname("label")
    self.pushbutton = qtwidgets.qpushbutton(self.centralwidget)
    self.pushbutton.setgeometry(qtcore.qrect(330, 260, 75, 23))
    self.pushbutton.setobjectname("pushbutton")
    self.lineedit = qtwidgets.qlineedit(self.centralwidget)
    self.lineedit.setgeometry(qtcore.qrect(280, 130, 151, 21))
    self.lineedit.setobjectname("lineedit")
    self.label_2 = qtwidgets.qlabel(self.centralwidget)
    self.label_2.setgeometry(qtcore.qrect(190, 180, 51, 31))
    self.label_2.setobjectname("label_2")
    self.lineedit_2 = qtwidgets.qlineedit(self.centralwidget)
    self.lineedit_2.setgeometry(qtcore.qrect(280, 190, 151, 21))
    self.lineedit_2.setobjectname("lineedit_2")
    self.pushbutton_2 = qtwidgets.qpushbutton(self.centralwidget)
    self.pushbutton_2.setgeometry(qtcore.qrect(200, 260, 75, 23))
    self.pushbutton_2.setobjectname("pushbutton_2")
    mainwindow.setcentralwidget(self.centralwidget)
    self.menubar = qtwidgets.qmenubar(mainwindow)
    self.menubar.setgeometry(qtcore.qrect(0, 0, 624, 23))
    self.menubar.setobjectname("menubar")
    mainwindow.setmenubar(self.menubar)
    self.statusbar = qtwidgets.qstatusbar(mainwindow)
    self.statusbar.setobjectname("statusbar")
    mainwindow.setstatusbar(self.statusbar)
 
    self.retranslateui(mainwindow)
    qtcore.qmetaobject.connectslotsbyname(mainwindow)
 
  def retranslateui(self, mainwindow):
    _translate = qtcore.qcoreapplication.translate
    mainwindow.setwindowtitle(_translate("mainwindow", "mainwindow"))
    self.label.settext(_translate("mainwindow", "  长"))
    self.pushbutton.settext(_translate("mainwindow", "确定"))
    self.label_2.settext(_translate("mainwindow", "  宽"))
    self.pushbutton_2.settext(_translate("mainwindow", "取消"))
 
if __name__ == "__main__":
  app = qapplication(sys.argv)
  mainwindow = qmainwindow()
  ui = ui_mainwindow()
  ui.setupui(mainwindow)
 
  ex = firstwindow()
  ex.btn.clicked.connect(mainwindow.show)
  ex.show()
 
  sys.exit(app.exec_())

以上这篇pyqt5使用按钮进行界面的跳转方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/pursuit_zhangyu/article/details/82916224