【问题标题】:PyQt5 relation between windows, first won't close窗口之间的PyQt5关系,首先不会关闭
【发布时间】:2020-07-24 02:00:20
【问题描述】:

我三周前开始涉足Linux,python3,从第二周开始也开始研究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 of test3.py ?

以及为什么如果我取消在第二个窗口上退出然后从第一个窗口重新退出,第二个窗口上的计时器从 0 重新开始,而第一个未关闭的窗口也不会停止其计时器,如果我在第一个未关闭的窗口上按“睡眠 5”,第一个和第二个窗口计时器都停止 5 秒?

这与父/子窗口关系有关吗?所有 .ui 文件都没有说父或子之类的内容?是别的吗?当第二个窗口打开时,如何让第一个窗口计时器停止(即确定关闭它)

【问题讨论】:

    标签: python-3.x qt user-interface widget pyqt5


    【解决方案1】:

    因为变量window没有在def的范围内声明:

    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
    

    同样的错误在Quitto.quitbuttonboxnonquit:

    def quitbuttonboxnonquit(self):
        print('return to main')
        # window.show() just delete this row
        self.close()
    

    【讨论】:

    • inside "def qpushbuttonqpressed(self)" window.hide() 按预期工作:隐藏主窗口。如何在 qpushbuttonqpressed 中声明窗口??
    • @pippo1980 你的代码中调用 window.hide() 在哪里?
    • 我编辑了代码,将“window.hide()”放在第 80 行左右,这样可以,但是 window.close() 不行
    猜你喜欢
    • 1970-01-01
    • 2020-10-17
    • 2023-01-24
    • 2021-05-09
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    相关资源
    最近更新 更多