Python PyQt5中弹出子窗口解决子窗口一闪而过的问题

来自:互联网
时间:2021-03-23
阅读:

方式一:槽函数中创建子窗口对象,赋值到普通变量

在主窗口添加按钮,并把按钮信号关联槽,在槽函数中创建子窗口对象赋值到普通变量,并调用其 show 方法。

from PyQt5.QtWidgets import *
import sys
 
class Main(QMainWindow):
  def __init__(self):
    super().__init__()
    self.setWindowTitle("主窗口")
    button = QPushButton("弹出子窗", self)
    button.clicked.connect(self.show_child)
 
  def show_child(self):
    child_window = Child()
    child_window.show()
 
class Child(QWidget):
  def __init__(self):
    super().__init__()
    self.setWindowTitle("我是子窗口啊")
 
# 运行主窗口
if __name__ == "__main__":
  app = QApplication(sys.argv)
 
  window = Main()
  window.show()
 
  sys.exit(app.exec_())

运行结果: 该段代码运行后,点击主窗口中的按钮,子窗口一闪而过。

方式二:槽函数中创建子窗口对象,赋值为对象属性

在主窗口添加按钮,并把按钮信号关联槽,在槽函数中创建子窗口对象并赋值为对象属性,并调用其 show 方法。

from PyQt5.QtWidgets import *
import sys
 
class Main(QMainWindow):
  def __init__(self):
    super().__init__()
    self.setWindowTitle("主窗口")
    button = QPushButton("弹出子窗", self)
    button.clicked.connect(self.show_child)
 
  def show_child(self):
    self.child_window = Child()
    self.child_window.show()
 
class Child(QWidget):
  def __init__(self):
    super().__init__()
    self.setWindowTitle("我是子窗口啊")
 
# 运行主窗口
if __name__ == "__main__":
  app = QApplication(sys.argv)
 
  window = Main()
  window.show()
 
  sys.exit(app.exec_())

运行结果: 该段代码运行后,点击主窗口中的按钮,子窗口正常打开,重复点击按钮,子窗口重复弹出。

方式三:在主窗口__init__方法中创建子窗

在主窗口__init__方法中创建子窗口对象并赋值为对象属性,添加按钮,并把按钮信号关联槽,在槽函数中调用子窗口对象的 show 方法。

from PyQt5.QtWidgets import *
import sys
 
class Main(QMainWindow):
  def __init__(self):
    super().__init__()
    self.setWindowTitle("主窗口")
    button = QPushButton("弹出子窗", self)
    button.clicked.connect(self.show_child)
    self.child_window = Child()
 
  def show_child(self):
    self.child_window.show()
 
class Child(QWidget):
  def __init__(self):
    super().__init__()
    self.setWindowTitle("我是子窗口啊")
 
# 运行主窗口
if __name__ == "__main__":
  app = QApplication(sys.argv)
 
  window = Main()
  window.show()
 
  sys.exit(app.exec_())

运行结果: 重复点击按钮,子窗口不重复弹出。

方式四:exec()方法

把例1的show()方法改为exec()方法

from PyQt5.QtWidgets import *
import sys
 
class Main(QMainWindow):
  def __init__(self):
    super().__init__()
    self.setWindowTitle("主窗口")
    button = QPushButton("弹出子窗", self)
    button.clicked.connect(self.show_child)
 
  def show_child(self):
    child_window = Child()
    child_window.exec()
 
class Child(QWidget):
  def __init__(self):
    super().__init__()
    self.setWindowTitle("我是子窗口啊")
 
# 运行主窗口
if __name__ == "__main__":
  app = QApplication(sys.argv)
 
  window = Main()
  window.show()
 
  sys.exit(app.exec_())

运行结果:子窗口顺利弹出,且不能重新选择父窗口

结论:

这里涉及到一个概念 模式对话框 与 非模式对话框 (modeless dialog | modal dialog)

模式对话框,就是在弹出窗口的时候,整个程序就被锁定了,处于等待状态,直到对话框被关闭。这时往往是需要对话框的返回值进行下面的操作。如:确认窗口(选择“是”或“否”)。
非模式对话框,在调用弹出窗口之后,调用即刻返回,继续下面的操作。这里只是一个调用指令的发出,不等待也不做任何处理。如:查找框。

show() ------  modeless dialog

exec() ------- modal dialog

方式一中 子窗口 通过 show() 方法显示,为非模态窗口,它的实例为父窗口show_child()方法中的局部变量,当窗口显示后,父窗口的show_child()方法继续执行,当方法运行完后,python的回收机制就把局部变量销毁了,相当于子窗口实例被销毁,故子窗口一闪而过; 方式二中 子窗口实例为 主窗口类的变量,当show_child()方法运行完后,主窗口对象依旧存在,子窗口实例也存在,故子窗口正常显示,但是每一次运行槽函数都会重新创建子窗口对象; 方式三中 子窗口实例为 主窗口类的变量,当show_child()方法运行完后,主窗口对象依旧存在,子窗口实例也存在,故子窗口正常显示,每一次show_child()函数,重新调用子窗口对象show_child()方法,不会创建新窗口,且可随意在父,子窗口间切换; 方式四中 子窗口 通过 exec() 方法显示,为模态窗口,虽然他为父窗口show_child()方法中的局部变量,由于阻塞的机制,父窗口show_child()并没有继续执行,故其不会像 例1 中 一闪而过,且不能在父,子窗口间切换;
返回顶部
顶部