按钮和功能在 PyQT5 应用程序的第二个 window 不起作用
Button and function doesn't work at second window of PyQT5 app
在我的应用程序中,我有 2 个 QMainWindows。 UI 文件由 pyuic5 生成并存储在单独的文件中。当我单击 MainWindow 中的按钮时,会打开第二个 window 称为 BlockWindow。在此 window 中 init func 存储方法“loaddata”并将其加载到 QTableWidget 中的数据。但是,当您打开第二个 window 时,什么也没有发生。但是,如果您单独调试文件 BlockWindow(second window) 它可以完美运行。你能指出问题到底是什么吗?
MainWindow.py
from MainWindowUI import Ui_MainWindow
from BlockWindowUI import Ui_BlockWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.loaddata)
self.ui.action.triggered.connect(self.openJournal)
self.ui.action_4.triggered.connect(exit)
self.ui.label_2 = QLabel('<h1 style="color: rgb(255, 255, 255);"></h1>')
self.loaddata()
def loaddata(self):
conn = pyodbc.connect(
"Driver={ODBC Driver 17 for SQL Server};Server=DESKTOP-49J58F3\SQLEXPRESS;Database=Plan;Trusted_Connection=yes;")
query = "SELECT nn, name, metering, volume, date_end, responsible, txt_name from dbo.Card_plan"
result = conn.execute(query).fetchall()
self.ui.tableWidget.clearSelection()
self.ui.tableWidget.setRowCount(0)
for row_number, row_data in enumerate(result):
self.ui.tableWidget.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.ui.tableWidget.setItem(row_number, column_number, QtWidgets.QTableWidgetItem(str(data)))
conn.commit()
def openJournal(self):
self.BlockWindow = QtWidgets.QMainWindow()
self.ui = Ui_BlockWindow()
self.ui.setupUi(self.BlockWindow)
self.BlockWindow.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
BlockWindow.py
from BlockWindowUI import *
class BlockWindow(QtWidgets.QMainWindow, Ui_BlockWindow):
def __init__(self):
super(BlockWindow, self).__init__()
self.ui = Ui_BlockWindow()
self.ui.setupUi(self)
self.ui.pushButton_3.clicked.connect(self.dels)
self.ui.pushButton_4.clicked.connect(BlockWindow.close)
self.ui.label_2 = QLabel('<h1 style="color: rgb(255, 255, 255);"></h1>')
self.loaddata()
def loaddata(self):
conn = pyodbc.connect(
"Driver={ODBC Driver 17 for SQL Server};Server=DESKTOP-49J58F3\SQLEXPRESS;Database=Plan;Trusted_Connection=yes;")
query = "SELECT * from dbo.Block"
result = conn.execute(query).fetchall()
self.ui.tableWidget.setRowCount(0)
for row_number, row_data in enumerate(result):
self.ui.tableWidget.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.ui.tableWidget.setItem(row_number, column_number, QtWidgets.QTableWidgetItem(str(data)))
conn.commit()
在您的 MainWindow
class 中尝试将您的 openJournal
方法更改为:
def openJournal(self):
self.BlockWindow = BlockWindow()
self.BlockWindow.show()
并且不要忘记将您的 class 导入到 MainWindow.py:
from BlockWindow import BlockWindow
在我的应用程序中,我有 2 个 QMainWindows。 UI 文件由 pyuic5 生成并存储在单独的文件中。当我单击 MainWindow 中的按钮时,会打开第二个 window 称为 BlockWindow。在此 window 中 init func 存储方法“loaddata”并将其加载到 QTableWidget 中的数据。但是,当您打开第二个 window 时,什么也没有发生。但是,如果您单独调试文件 BlockWindow(second window) 它可以完美运行。你能指出问题到底是什么吗?
MainWindow.py
from MainWindowUI import Ui_MainWindow
from BlockWindowUI import Ui_BlockWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.loaddata)
self.ui.action.triggered.connect(self.openJournal)
self.ui.action_4.triggered.connect(exit)
self.ui.label_2 = QLabel('<h1 style="color: rgb(255, 255, 255);"></h1>')
self.loaddata()
def loaddata(self):
conn = pyodbc.connect(
"Driver={ODBC Driver 17 for SQL Server};Server=DESKTOP-49J58F3\SQLEXPRESS;Database=Plan;Trusted_Connection=yes;")
query = "SELECT nn, name, metering, volume, date_end, responsible, txt_name from dbo.Card_plan"
result = conn.execute(query).fetchall()
self.ui.tableWidget.clearSelection()
self.ui.tableWidget.setRowCount(0)
for row_number, row_data in enumerate(result):
self.ui.tableWidget.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.ui.tableWidget.setItem(row_number, column_number, QtWidgets.QTableWidgetItem(str(data)))
conn.commit()
def openJournal(self):
self.BlockWindow = QtWidgets.QMainWindow()
self.ui = Ui_BlockWindow()
self.ui.setupUi(self.BlockWindow)
self.BlockWindow.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
BlockWindow.py
from BlockWindowUI import *
class BlockWindow(QtWidgets.QMainWindow, Ui_BlockWindow):
def __init__(self):
super(BlockWindow, self).__init__()
self.ui = Ui_BlockWindow()
self.ui.setupUi(self)
self.ui.pushButton_3.clicked.connect(self.dels)
self.ui.pushButton_4.clicked.connect(BlockWindow.close)
self.ui.label_2 = QLabel('<h1 style="color: rgb(255, 255, 255);"></h1>')
self.loaddata()
def loaddata(self):
conn = pyodbc.connect(
"Driver={ODBC Driver 17 for SQL Server};Server=DESKTOP-49J58F3\SQLEXPRESS;Database=Plan;Trusted_Connection=yes;")
query = "SELECT * from dbo.Block"
result = conn.execute(query).fetchall()
self.ui.tableWidget.setRowCount(0)
for row_number, row_data in enumerate(result):
self.ui.tableWidget.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.ui.tableWidget.setItem(row_number, column_number, QtWidgets.QTableWidgetItem(str(data)))
conn.commit()
在您的 MainWindow
class 中尝试将您的 openJournal
方法更改为:
def openJournal(self):
self.BlockWindow = BlockWindow()
self.BlockWindow.show()
并且不要忘记将您的 class 导入到 MainWindow.py:
from BlockWindow import BlockWindow