我正在为初学者学习Python教程。本教程让我编写代码来显示一个名为“realworld.ui”的窗口。我根据教程编写了代码,该教程在导师的计算机系统上显示了窗口。当我在我的系统上运行这个应用程序时,我得到了这个运行时错误。根据我在编码中所看到的,似乎只有一行代码创建了这个应用程序。这个错误似乎告诉我,这条线被多次调用。看起来这是编码中的递归问题。我是Python新手,我只想让这一行执行1次。
line 32, in Main
app = QApplication(sys.argv)
RuntimeError: Please destroy the QApplication singleton before creating a new QApplication instance.
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6 import QtWidgets
from PySide6.QtCore import *
from PySide6 import QtCore
from PySide6.QtWidgets import QMainWindow
from PySide6.QtWidgets import QApplication
import sys
from os import path
from PySide6.QtUiTools import loadUiType
import sqlite3
folder_path = '/Users/emad-ud-deen/Development/Python/Practice/Real World Database App/'
# Load the window.
#-----------------
FORM_CLASS, _=loadUiType(path.join(path.dirname('__file__'), folder_path + 'real_world.ui'))
class MainWindow(QMainWindow, FORM_CLASS):
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
QMainWindow.__init__(self)
self.setupUi(self)
self.HandleButtonClicks()
def HandleButtonClicks(self):
print('At HandleButtonClicks')
def Main():
app = QApplication(sys.argv)
window = Main()
window.show()
app.exec()
if __name__ == '__main__':
Main()
real_world.ui编码
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>747</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Parts Inventory Manager</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="label_background">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>721</width>
<height>541</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap>sky3.jpg</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
</widget>
<widget class="QTabWidget" name="tabWidget_inventory">
<property name="geometry">
<rect>
<x>50</x>
<y>130</y>
<width>621</width>
<height>311</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="tabPosition">
<enum>QTabWidget::North</enum>
</property>
<property name="tabShape">
<enum>QTabWidget::Rounded</enum>
</property>
<property name="currentIndex">
<number>2</number>
</property>
<widget class="QWidget" name="tab_inventory_details">
<attribute name="title">
<string>Inventory Details</string>
</attribute>
<widget class="QTableWidget" name="tableWidget_inventory_details">
<property name="geometry">
<rect>
<x>17</x>
<y>40</y>
<width>581</width>
<height>221</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: wheat;
font: 13pt ".AppleSystemUIFont";</string>
</property>
<column>
<property name="text">
<string>ID</string>
</property>
</column>
<column>
<property name="text">
<string>Reference</string>
</property>
</column>
<column>
<property name="text">
<string>Part Name</string>
</property>
</column>
<column>
<property name="text">
<string>Min Area</string>
</property>
</column>
<column>
<property name="text">
<string>Max Area</string>
</property>
</column>
<column>
<property name="text">
<string>No. Of Holes</string>
</property>
</column>
<column>
<property name="text">
<string>Min Diameter</string>
</property>
</column>
<column>
<property name="text">
<string>Max Diameter</string>
</property>
</column>
<column>
<property name="text">
<string>Count</string>
</property>
</column>
</widget>
<widget class="QSpinBox" name="spinBox_count_inventory_details">
<property name="geometry">
<rect>
<x>300</x>
<y>10</y>
<width>42</width>
<height>21</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 13pt ".AppleSystemUIFont";</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_search_inventory_details">
<property name="geometry">
<rect>
<x>499</x>
<y>6</y>
<width>100</width>
<height>32</height>
</rect>
</property>
<property name="text">
<string>Search</string>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
<widget class="QLabel" name="label_search_for_count_inventory_details">
<property name="geometry">
<rect>
<x>17</x>
<y>10</y>
<width>281</width>
<height>21</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 15pt ".AppleSystemUIFont";</string>
</property>
<property name="text">
<string>Search for references that are <= a count:</string>
</property>
</widget>
</widget>
<widget class="QWidget" name="inventory_statistics">
<attribute name="title">
<string>Inventory Statistics</string>
</attribute>
<widget class="QTableWidget" name="tableWidget_top_3_references">
<property name="geometry">
<rect>
<x>17</x>
<y>110</y>
<width>441</width>
<height>151</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: wheat;
font: 13pt ".AppleSystemUIFont";</string>
</property>
<column>
<property name="text">
<string>Reference</string>
</property>
</column>
<column>
<property name="text">
<string>Part Name</string>
</property>
</column>
<column>
<property name="text">
<string>Count</string>
</property>
</column>
</widget>
<widget class="QLabel" name="label_top_3_references">
<property name="geometry">
<rect>
<x>17</x>
<y>90</y>
<width>291</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Top 3 references with minimum inventory level:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QPushButton" name="pushButton_check">
<property name="geometry">
<rect>
<x>470</x>
<y>170</y>
<width>100</width>
<height>32</height>
</rect>
</property>
<property name="text">
<string>Check</string>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
<widget class="QFrame" name="frame_statistics">
<property name="geometry">
<rect>
<x>17</x>
<y>10</y>
<width>441</width>
<height>71</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: wheat;
</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QLabel" name="label_maximum_holes_amount_2">
<property name="geometry">
<rect>
<x>360</x>
<y>40</y>
<width>21</width>
<height>20</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 13pt ".AppleSystemUIFont";</string>
</property>
<property name="text">
<string>0</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_maximum_holes">
<property name="geometry">
<rect>
<x>220</x>
<y>40</y>
<width>141</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>Max. Number of Holes:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_minimum_holes">
<property name="geometry">
<rect>
<x>220</x>
<y>10</y>
<width>141</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Min. Number of Holes:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_minimum_holes_amount">
<property name="geometry">
<rect>
<x>360</x>
<y>10</y>
<width>21</width>
<height>20</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 13pt ".AppleSystemUIFont";</string>
</property>
<property name="text">
<string>0</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_total_part_types_amount">
<property name="geometry">
<rect>
<x>120</x>
<y>40</y>
<width>21</width>
<height>20</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 13pt ".AppleSystemUIFont";</string>
</property>
<property name="text">
<string>0</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_total_references_amount">
<property name="geometry">
<rect>
<x>120</x>
<y>10</y>
<width>21</width>
<height>20</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 13pt ".AppleSystemUIFont";</string>
</property>
<property name="text">
<string>0</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_total_references">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>111</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Total References:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_total_part_types">
<property name="geometry">
<rect>
<x>10</x>
<y>40</y>
<width>111</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Total Part Types:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</widget>
</widget>
<widget class="QWidget" name="edit_inventory">
<attribute name="title">
<string>Edit Inventory</string>
</attribute>
<widget class="QPushButton" name="pushButton_search_edit_inventory">
<property name="geometry">
<rect>
<x>499</x>
<y>6</y>
<width>100</width>
<height>32</height>
</rect>
</property>
<property name="text">
<string>Search</string>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
<widget class="QFrame" name="frame">
<property name="geometry">
<rect>
<x>22</x>
<y>50</y>
<width>571</width>
<height>211</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: wheat;
font: 13pt ".AppleSystemUIFont";</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QLineEdit" name="lineEdit_minimum_diameter">
<property name="geometry">
<rect>
<x>464</x>
<y>80</y>
<width>81</width>
<height>21</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: cornsilk;
font: 13pt ".AppleSystemUIFont";</string>
</property>
</widget>
<widget class="QLabel" name="label_edit_count">
<property name="geometry">
<rect>
<x>354</x>
<y>140</y>
<width>101</width>
<height>16</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 15pt ".AppleSystemUIFont";</string>
</property>
<property name="text">
<string>Count:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_part_name">
<property name="geometry">
<rect>
<x>134</x>
<y>80</y>
<width>181</width>
<height>21</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: cornsilk;
font: 13pt ".AppleSystemUIFont";</string>
</property>
</widget>
<widget class="QLabel" name="label_maximum_area">
<property name="geometry">
<rect>
<x>24</x>
<y>140</y>
<width>101</width>
<height>16</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 15pt ".AppleSystemUIFont";</string>
</property>
<property name="text">
<string>Max Area:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_minimum_area">
<property name="geometry">
<rect>
<x>134</x>
<y>110</y>
<width>81</width>
<height>21</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: cornsilk;
font: 13pt ".AppleSystemUIFont";</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_reference">
<property name="geometry">
<rect>
<x>134</x>
<y>50</y>
<width>81</width>
<height>21</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: cornsilk;
font: 13pt ".AppleSystemUIFont";</string>
</property>
</widget>
<widget class="QLabel" name="label_edit_minimum_diameter">
<property name="geometry">
<rect>
<x>354</x>
<y>80</y>
<width>101</width>
<height>16</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 15pt ".AppleSystemUIFont";</string>
</property>
<property name="text">
<string>Min Diameter:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_reference_maximum_diameter">
<property name="geometry">
<rect>
<x>464</x>
<y>110</y>
<width>81</width>
<height>21</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: cornsilk;
font: 13pt ".AppleSystemUIFont";</string>
</property>
</widget>
<widget class="QLabel" name="label_minimum_area">
<property name="geometry">
<rect>
<x>24</x>
<y>110</y>
<width>101</width>
<height>16</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 15pt ".AppleSystemUIFont";</string>
</property>
<property name="text">
<string>Min Area:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_edit_part_name">
<property name="geometry">
<rect>
<x>24</x>
<y>80</y>
<width>101</width>
<height>16</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 15pt ".AppleSystemUIFont";</string>
</property>
<property name="text">
<string>Part Name:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_number_of_holes">
<property name="geometry">
<rect>
<x>464</x>
<y>50</y>
<width>81</width>
<height>21</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: cornsilk;
font: 13pt ".AppleSystemUIFont";</string>
</property>
</widget>
<widget class="QLabel" name="label_edit_number_of_holes">
<property name="geometry">
<rect>
<x>334</x>
<y>50</y>
<width>121</width>
<height>20</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 15pt ".AppleSystemUIFont";</string>
</property>
<property name="text">
<string>Number of Holes:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_maximum_area">
<property name="geometry">
<rect>
<x>134</x>
<y>140</y>
<width>81</width>
<height>21</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: cornsilk;
font: 13pt ".AppleSystemUIFont";</string>
</property>
</widget>
<widget class="QLabel" name="label_edit_maximum_diameter">
<property name="geometry">
<rect>
<x>354</x>
<y>110</y>
<width>101</width>
<height>16</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 15pt ".AppleSystemUIFont";</string>
</property>
<property name="text">
<string>Max Diameter:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_edit_reference">
<property name="geometry">
<rect>
<x>24</x>
<y>50</y>
<width>101</width>
<height>16</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 15pt ".AppleSystemUIFont";</string>
</property>
<property name="text">
<string>Reference:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_edit_ID_value">
<property name="geometry">
<rect>
<x>135</x>
<y>19</y>
<width>101</width>
<height>20</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 15pt ".AppleSystemUIFont";
background-color:BurlyWood;</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_edit_reference_3">
<property name="geometry">
<rect>
<x>26</x>
<y>20</y>
<width>101</width>
<height>16</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 15pt ".AppleSystemUIFont";</string>
</property>
<property name="text">
<string>ID:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QSpinBox" name="spinBox_count">
<property name="geometry">
<rect>
<x>463</x>
<y>140</y>
<width>81</width>
<height>22</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: cornsilk;
font: 13pt ".AppleSystemUIFont";</string>
</property>
</widget>
<widget class="QFrame" name="frame_natigation">
<property name="geometry">
<rect>
<x>10</x>
<y>170</y>
<width>291</width>
<height>31</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QPushButton" name="pushButton_go_to_beginning">
<property name="geometry">
<rect>
<x>10</x>
<y>0</y>
<width>61</width>
<height>32</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string><<</string>
</property>
<property name="default">
<bool>false</bool>
</property>
</widget>
<widget class="QPushButton" name="pushButton_previous">
<property name="geometry">
<rect>
<x>80</x>
<y>0</y>
<width>61</width>
<height>32</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string><</string>
</property>
<property name="default">
<bool>false</bool>
</property>
</widget>
<widget class="QPushButton" name="pushButton_next">
<property name="geometry">
<rect>
<x>150</x>
<y>0</y>
<width>61</width>
<height>32</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string>></string>
</property>
<property name="default">
<bool>false</bool>
</property>
</widget>
<widget class="QPushButton" name="pushButton_go_to_end">
<property name="geometry">
<rect>
<x>220</x>
<y>0</y>
<width>61</width>
<height>32</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string>>></string>
</property>
<property name="default">
<bool>false</bool>
</property>
</widget>
</widget>
<widget class="QFrame" name="frame_database_controls">
<property name="geometry">
<rect>
<x>310</x>
<y>170</y>
<width>251</width>
<height>31</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QPushButton" name="pushButton_update_row">
<property name="geometry">
<rect>
<x>10</x>
<y>0</y>
<width>71</width>
<height>32</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string>Update</string>
</property>
<property name="default">
<bool>false</bool>
</property>
</widget>
<widget class="QPushButton" name="pushButton_delete_row">
<property name="geometry">
<rect>
<x>90</x>
<y>0</y>
<width>71</width>
<height>32</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string>Delete</string>
</property>
<property name="default">
<bool>false</bool>
</property>
</widget>
<widget class="QPushButton" name="pushButton_insert_row">
<property name="geometry">
<rect>
<x>170</x>
<y>0</y>
<width>71</width>
<height>32</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string>Add</string>
</property>
<property name="default">
<bool>false</bool>
</property>
</widget>
</widget>
</widget>
</widget>
</widget>
<widget class="QLabel" name="label_title">
<property name="geometry">
<rect>
<x>0</x>
<y>30</y>
<width>721</width>
<height>71</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>64</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">color: gold;
background-color: SaddleBrown;</string>
</property>
<property name="text">
<string>Parts Inventory Manager</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QPushButton" name="pushButton_refresh">
<property name="geometry">
<rect>
<x>570</x>
<y>451</y>
<width>100</width>
<height>32</height>
</rect>
</property>
<property name="text">
<string>Refresh</string>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>747</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
发布于 2022-04-24 19:44:54
def Main():
app = QApplication(sys.argv)
window = Main()
...
是递归的,我想应该是window = MainWindow()
。
https://stackoverflow.com/questions/71991708
复制相似问题