4

使用 PyQt 快速搭建带有 GUI 的应用(8)–多窗口之间跳转 | 文艺数学君

 3 years ago
source link: https://mathpretty.com/13702.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

使用 PyQt 快速搭建带有 GUI 的应用(8)–多窗口之间跳转

所属分类:Python库介绍

摘要在实际应用编写中,可能会出现多个界面之间相互的跳转。例如有一个主界面,点击按钮之后会有另外一个窗口显示。在本文中,我们就会介绍这样的例子。包含一个主要 QDialog,点击按钮会出现一个新的 QDialog。

在实际应用编写中,可能会出现多个界面之间相互的跳转。例如有一个主界面,点击按钮之后会有另外一个窗口显示。在本文中,我们就会介绍这样的例子。包含一个主要 QDialog,点击按钮会出现一个新的 QDialog。

因为本文的内容会比较简短。如果对 PyQt 不是很熟悉,建议先查看以下的文章:

PyQt 多窗口跳转例子

主要会包含两个步骤,首先是创建一个「弹出界面」,接着创建一个「主页面」。主界面上会有一个按钮,点击按钮,会出现「弹出界面」。

创建一个弹出界面

首先我们创建一个弹出的界面。因为只是为了说明窗口之间的切换,所以我们这个界面就制作的比较简单。上面会包含一个按钮,点击会出现一个 QMessageBox

  1. import sys
  2. from PyQt5.QtWidgets import QFormLayout, QApplication, QDialog, QPushButton, QMessageBox
  3. class new_Dialog(QDialog):
  4.     """弹出的界面
  5.     def __init__(self, parent=None):
  6.         super().__init__(parent)
  7.         form = QFormLayout()
  8.         self.qpush_messageBox = QPushButton('出现弹窗')
  9.         form.addRow('二号窗口', self.qpush_messageBox)
  10.         self.setLayout(form)
  11.         # 绑定按钮
  12.         self.qpush_messageBox.clicked.connect(self.message_box)
  13.     def message_box(self):
  14.         QMessageBox.about(self, 'Title', 'Hello, World.')

上面运行之后,会有如下的界面。点击按钮出现弹窗:

使用 PyQt 快速搭建带有 GUI 的应用(8)--多窗口之间跳转

创建一个主界面

接下来我们创建「主界面」。说是主界面,其实也非常简单。我们主要要实现的是通过点击按钮,显示上面的「弹出界面」。主界面代码如下所示:

  1. class Dialog(QDialog):
  2.     """主要界面
  3.     def __init__(self, parent=None):
  4.         super().__init__(parent)
  5.         form = QFormLayout()
  6.         self.qpush_openwin = QPushButton('打开一个新的窗口')
  7.         form.addRow('点击按钮', self.qpush_openwin)
  8.         self.setLayout(form)
  9.         # 绑定按钮
  10.         self.qpush_openwin.clicked.connect(self.select_file)
  11.     def select_file(self):
  12.         """选择文件
  13.         new_win = new_Dialog()
  14.         new_win.show() # 一定要加 show, 否则原始窗口无法点击
  15.         new_win.exec()

生成的界面如下所示:

使用 PyQt 快速搭建带有 GUI 的应用(8)--多窗口之间跳转

完整代码-窗口跳转

上面我们创建了两个窗口,现在看一下最终的效果。完整代码如下所示:

  1. import sys
  2. from PyQt5.QtWidgets import QFormLayout, QApplication, QDialog, QPushButton, QMessageBox
  3. class new_Dialog(QDialog):
  4.     """弹出的界面
  5.     def __init__(self, parent=None):
  6.         super().__init__(parent)
  7.         form = QFormLayout()
  8.         self.qpush_messageBox = QPushButton('出现弹窗')
  9.         form.addRow('二号窗口', self.qpush_messageBox)
  10.         self.setLayout(form)
  11.         # 绑定按钮
  12.         self.qpush_messageBox.clicked.connect(self.message_box)
  13.     def message_box(self):
  14.         QMessageBox.about(self, 'Title', 'Hello, World.')
  15. class Dialog(QDialog):
  16.     """主要界面
  17.     def __init__(self, parent=None):
  18.         super().__init__(parent)
  19.         form = QFormLayout()
  20.         self.qpush_openwin = QPushButton('打开一个新的窗口')
  21.         form.addRow('点击按钮', self.qpush_openwin)
  22.         self.setLayout(form)
  23.         # 绑定按钮
  24.         self.qpush_openwin.clicked.connect(self.select_file)
  25.     def select_file(self):
  26.         """选择文件
  27.         new_win = new_Dialog()
  28.         new_win.show() # 一定要加 show, 否则原始窗口无法点击
  29.         new_win.exec()
  30. if __name__ == '__main__':
  31.     app = QApplication(sys.argv)
  32.     dlg = Dialog()
  33.     dlg.show()
  34.     sys.exit(app.exec_())

最终生成的效果如下所示:

使用 PyQt 快速搭建带有 GUI 的应用(8)--多窗口之间跳转
  • 微信公众号
  • 关注微信公众号
  • weinxin
  • QQ群
  • 我们的QQ群号
  • weinxin

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK