欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

pyqt 多窗口之间的相互调用方法

程序员文章站 2022-11-14 16:19:45
* 在编程开发中,一个程序不可避免的需要多窗口操作来实现具体的功能。 实现此功能的基本步骤(以三个窗口为例,使用主窗口调用其它两个窗口) # 主窗口 from...

* 在编程开发中,一个程序不可避免的需要多窗口操作来实现具体的功能。

实现此功能的基本步骤(以三个窗口为例,使用主窗口调用其它两个窗口)

# 主窗口
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(70, 180, 75, 23))
    self.pushbutton.setobjectname("pushbutton")
 
    self.pushbutton_2 = qtwidgets.qpushbutton(self.centralwidget)
    self.pushbutton_2.setgeometry(qtcore.qrect(250, 180, 75, 23))
    self.pushbutton_2.setobjectname("pushbutton_2")
 
    mainwindow.setcentralwidget(self.centralwidget)
    self.menubar = qtwidgets.qmenubar(mainwindow)
    self.menubar.setgeometry(qtcore.qrect(0, 0, 800, 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.pushbutton.settext(_translate("mainwindow", "打开窗口1"))
    self.pushbutton_2.settext(_translate("mainwindow", "打开窗口2 "))
 
# 窗口1
class ui_dialog(object):
  def setupui(self, dialog):
    dialog.setobjectname("dialog")
    dialog.resize(400, 300)
    self.buttonbox = qtwidgets.qdialogbuttonbox(dialog)
    self.buttonbox.setgeometry(qtcore.qrect(30, 240, 341, 32))
    self.buttonbox.setorientation(qtcore.qt.horizontal)
    self.buttonbox.setstandardbuttons(qtwidgets.qdialogbuttonbox.cancel|qtwidgets.qdialogbuttonbox.ok)
    self.buttonbox.setobjectname("buttonbox")
    self.label = qtwidgets.qlabel(dialog)
    self.label.setgeometry(qtcore.qrect(140, 100, 54, 12))
    self.label.setobjectname("label")
 
    self.retranslateui(dialog)
    self.buttonbox.accepted.connect(dialog.accept)
    self.buttonbox.rejected.connect(dialog.reject)
    qtcore.qmetaobject.connectslotsbyname(dialog)
 
  def retranslateui(self, dialog):
    _translate = qtcore.qcoreapplication.translate
    dialog.setwindowtitle(_translate("dialog", "dialog"))
    self.label.settext(_translate("dialog", "这是窗口1"))
# 窗口2
 
class ui_form(object):
  def setupui(self, form):
    form.setobjectname("form")
    form.resize(400, 300)
    self.label = qtwidgets.qlabel(form)
    self.label.setgeometry(qtcore.qrect(140, 140, 54, 12))
    self.label.setobjectname("label")
 
    self.retranslateui(form)
    qtcore.qmetaobject.connectslotsbyname(form)
 
  def retranslateui(self, form):
    _translate = qtcore.qcoreapplication.translate
    form.setwindowtitle(_translate("form", "form"))
    self.label.settext(_translate("form", "这是窗口2"))
 

主程序入口:

# 主程序
class mainwindow(qmainwindow, untitled.ui_mainwindow):
  def __init__(self):
    super(mainwindow, self).__init__()
    self.setupui(self)
    self.window2 = ui_dialog()
    self.window2.setupui()
    self.window3 = ui_form()
    self.window3.setupui()
    self.pushbutton.clicked.connect(self.window2.show)# 绑定窗口2 
    self.pushbutton_2.clicked.connect(self.window3.show) # 绑定窗口3
 
 
if __name__ == '__main__':
  app = qapplication(sys.argv)
  mainwindow = mainwindow()
  mainwindow.show()
  sys.exit(app.exec_())

以上实现主窗口通过按钮弹出窗口1和窗口2

下面实现通过窗口按钮打开文件资源管理器,实现获取文件相关信息的功能:

1. 在主窗口中添加一个按钮:

class ui_mainwindow(object):
  def setupui(self, mainwindow):
    ......
    self.pushbutton_3 = qtwidgets.qpushbutton(self.centralwidget)
    self.pushbutton_3.setgeometry(qtcore.qrect(420, 180, 75, 23))
    self.pushbutton_3.setobjectname("pushbutton_3")
  def retranslateui(self, mainwindow):
    ......
    self.pushbutton_3.settext(_translate("mainwindow", "打开目录"))

2.主程序中添加:

# 主程序
class mainwindow(qmainwindow, untitled.ui_mainwindow):
  def __init__(self):
    super(mainwindow, self).__init__()
    self.setupui(self)
    self.window2 = ui_dialog()
    self.window2.setupui()
    self.window3 = ui_form()
    self.window3.setupui()
    self.pushbutton.clicked.connect(self.window2.show)# 绑定窗口2 
    self.pushbutton_2.clicked.connect(self.window3.show) # 绑定窗口3
 
    self.pushbutton_3.clicked.connect(self.gefilename) # 新增加的
  # 新增加的
  def gefilename(self):
    filename = qfiledialog.getopenfilename()
    return filename
 
 
if __name__ == '__main__':
  app = qapplication(sys.argv)
  mainwindow = mainwindow()
  mainwindow.show()
  sys.exit(app.exec_())

即可完成上述功能。

以上这篇pyqt 多窗口之间的相互调用方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。