[英]PyQt5 relation between windows, first won't close
I started treading into Linux, python3 three weeks ago, and from the second week started to look at PyQt5 too.三周前我开始涉足Linux,python3,从第二周开始也开始关注PyQt5。
There many tutorials and book, to me Qt documentation seems written in Klingon, so I am trying to follow有很多教程和书籍,对我来说 Qt 文档似乎是用克林贡语写的,所以我试图遵循
examples found on the web. web 上的示例。
The last problem I am facing is: given:我面临的最后一个问题是:给定:
test3.py测试3.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 8 14:25:12 2020
@author: Pietro
"""
import sys
from PyQt5 import QtWidgets, uic
from PyQt5.QtCore import QTimer
import time
class Quitto(QtWidgets.QDialog):
def quitbuttonboxquit(self):
print('system exit')
app.quit()
def quitbuttonboxnonquit(self):
print('return to main')
window.show()
self.close()
def __init__(self):
super(Quitto, self).__init__()
uic.loadUi('exitdialog3.ui', self)
self.center()
self.show()
print('inside quitting2 ' *5)
self.QuitbuttonBox.accepted.connect(self.quitbuttonboxquit)
self.QuitbuttonBox.rejected.connect(self.quitbuttonboxnonquit)
self.counter = 0
self.timer = QTimer()
self.timer.setInterval(1000)
self.timer.timeout.connect(self.recurring_timer)
self.timer.start()
def center(self):
qr = self.frameGeometry()
cp = QtWidgets.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def closeEvent(self, event):
event.ignore()
def recurring_timer(self):
self.counter +=1
self.label2.setText("TTCounter: %d" % self.counter)
class Menu(QtWidgets.QMainWindow):
def __init__(self):
super(Menu, self).__init__()
uic.loadUi('main2.ui', self)
self.ButtonQ.clicked.connect(self.qpushbuttonqpressed)
self.center()
self.show()
self.counter = 0
self.timer = QTimer()
self.timer.setInterval(1000)
self.timer.timeout.connect(self.recurring_timer)
self.timer.start()
self.ButtonA.clicked.connect(self.qpushbuttonapressed)
def center(self):
qr = self.frameGeometry()
cp = QtWidgets.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def qpushbuttonqpressed(self): #This is executed when the button is pressed
print('buttonQ pressed' *5)
# window.close() # doens't work, wondow doesn't close
window.hide() # this one does, windows hide
self.pippo=Quitto()
self.pippo.show()
def qpushbuttonapressed(self): #This is executed when the button is pressed
print('buttonA pressed' *5)
self.oh_no()
def closeEvent(self, event): #Your desired functionality here
event.ignore()
def recurring_timer(self):
self.counter +=1
self.label2.setText("TTCounter: %d" % self.counter)
def oh_no(self):
time.sleep(5)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
sshFile="coffee.qss"
window=Menu()
sys.exit(app.exec_())
and the two.ui files,和 two.ui 文件,
main2.ui main2.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="windowModality">
<enum>Qt::WindowModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>544</width>
<height>686</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="windowIcon">
<iconset resource="resource001.qrc">
<normaloff>:/main/python.png</normaloff>
<normalon>:/main/python.png</normalon>
<disabledoff>:/main/python.png</disabledoff>
<disabledon>:/main/python.png</disabledon>
<activeoff>:/main/python.png</activeoff>
<activeon>:/main/python.png</activeon>
<selectedoff>:/main/python.png</selectedoff>
<selectedon>:/main/python.png</selectedon>:/main/python.png</iconset>
</property>
<property name="styleSheet">
<string notr="true">QPushButton{
background-color: #9de650;
}
QPushButton:hover{
background-color: green;
}
QPushButton#ButtonQ{
background-color: orange;
}
QPushButton#ButtonQ:hover{
background-color: red;
}</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>20</x>
<y>0</y>
<width>471</width>
<height>71</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>16</pointsize>
<weight>75</weight>
<bold>true</bold>
<underline>true</underline>
</font>
</property>
<property name="text">
<string>House-Buying-Menu</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QPushButton" name="ButtonA">
<property name="geometry">
<rect>
<x>170</x>
<y>200</y>
<width>151</width>
<height>81</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>20</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>SLEEP 5</string>
</property>
</widget>
<widget class="QPushButton" name="ButtonB">
<property name="geometry">
<rect>
<x>170</x>
<y>310</y>
<width>151</width>
<height>81</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>20</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>B</string>
</property>
</widget>
<widget class="QPushButton" name="ButtonC">
<property name="geometry">
<rect>
<x>170</x>
<y>420</y>
<width>151</width>
<height>81</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>20</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>C</string>
</property>
</widget>
<widget class="QPushButton" name="ButtonQ">
<property name="geometry">
<rect>
<x>170</x>
<y>550</y>
<width>151</width>
<height>81</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>20</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>QUIT</string>
</property>
</widget>
<widget class="QLabel" name="label2">
<property name="geometry">
<rect>
<x>120</x>
<y>80</y>
<width>221</width>
<height>91</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>20</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string/>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>544</width>
<height>29</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources>
<include location="resource001.qrc"/>
</resources>
<connections/>
</ui>
exitdialog3.ui退出对话框3.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="windowModality">
<enum>Qt::WindowModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>720</width>
<height>655</height>
</rect>
</property>
<property name="font">
<font>
<italic>true</italic>
</font>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<widget class="QDialogButtonBox" name="QuitbuttonBox">
<property name="geometry">
<rect>
<x>80</x>
<y>560</y>
<width>341</width>
<height>32</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>18</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">QPushButton{
background-color: #17eb3e;
}
QPushButton:hover{
background-color: red;
}
</string>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>100</x>
<y>140</y>
<width>491</width>
<height>341</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">image: url(:/main/alert.png);</string>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>210</x>
<y>510</y>
<width>271</width>
<height>41</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>18</pointsize>
<weight>75</weight>
<italic>false</italic>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">QLabel {
color : red
}</string>
</property>
<property name="text">
<string>Are you sure to Quit ?!?</string>
</property>
</widget>
<widget class="QLabel" name="label2">
<property name="geometry">
<rect>
<x>190</x>
<y>40</y>
<width>321</width>
<height>101</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>20</pointsize>
<weight>75</weight>
<italic>false</italic>
<bold>true</bold>
</font>
</property>
<property name="text">
<string/>
</property>
</widget>
</widget>
<resources>
<include location="resource001.qrc"/>
</resources>
<connections>
<connection>
<sender>QuitbuttonBox</sender>
<signal>accepted()</signal>
<receiver>Dialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>QuitbuttonBox</sender>
<signal>rejected()</signal>
<receiver>Dialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
I don't understand why pressing Quit on the first window doesn't close it despite我不明白为什么在第一个 window 上按下 Quit 并没有关闭它,尽管
window.close()
#line 80 of test3.py? window.close()
#line 80 of test3.py?
and why if I cancel quitting on the second window and then re-quit from the first window the timer on the second window restarts from 0, while the first window that doesn't close doesn't stop its timer too and if I press 'sleep 5' on the first window that doesn't close, both first and second window timers stop for 5 seconds? and why if I cancel quitting on the second window and then re-quit from the first window the timer on the second window restarts from 0, while the first window that doesn't close doesn't stop its timer too and if I press '在第一个没有关闭的 window 上睡眠 5',第一个和第二个 window 计时器停止 5 秒?
Is it something about the parent/child window relation?是否与父/子 window 关系有关? None of the.ui files says anything like Parent or Child?没有一个 .ui 文件说像 Parent 或 Child 之类的东西? Is it something else?是别的吗? How could I make the first window timer stop (ie closing it for sure) while the second window is on我怎样才能让第一个 window 计时器停止(即确定关闭它)而第二个 window 开启
Because the variable window
hadn't been declared in the scope of the def
:因为变量window
没有在def
的 scope 中声明:
def qpushbuttonqpressed(self): #This is executed when the button is pressed
print('buttonQ pressed' *5)
self.close() # this is correct
self.pippo=Quitto()
self.pippo.exec() # use exec() to display the dialog
self.show() # When the dialog will be closed we show the window again
The same mistake is in Quitto.quitbuttonboxnonquit
:同样的错误出现在Quitto.quitbuttonboxnonquit
中:
def quitbuttonboxnonquit(self):
print('return to main')
# window.show() just delete this row
self.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.