Я начал наступать на Linux, python3 три недели go, и со второй недели тоже начал смотреть на PyQt5.
Там много учебников и книг, мне кажется, что документация по Qt написано на клингоне, поэтому я пытаюсь следовать
примерам, найденным в Интернете.
Последняя проблема, с которой я сталкиваюсь: дано:
test3.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_())
и два файла .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
<?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>
Я не понимаю, почему нажатие Выход в первом окне не закрывает его, несмотря на
window.close()
# line 80 из test3.py?
и почему, если я отменяю выход во втором окне, а затем снова завершаю работу с В первом окне таймер во втором окне перезапускается с 0, в то время как первое окно, которое не закрывается, тоже не останавливает свой таймер, и если я нажму 'sleep 5' в первом окне, которое не закрывается, как первое, так и таймеры второго окна останавливаются на 5 секунд?
Это что-то касается отношения родительского / дочернего окна? Ни один из файлов .ui не говорит ничего похожего на Parent или Child? Это что-то еще? Как сделать так, чтобы таймер первого окна останавливался (то есть закрывался наверняка), пока второе окно включено