【发布时间】:2021-01-29 18:39:25
【问题描述】:
在 PyQt5 中,我有一个从数据库读取信息并将该信息添加到小部件的窗口。还有第二个窗口,打开时会向数据库中添加一个新表。我要做的是在关闭辅助窗口时重新加载主窗口。我不想将辅助窗口更改为QDialoge,需要使用.show(),而不是.exec_()。有什么办法可以做到这一点。
这是我的代码:
import sys
import sqlite3
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QPushButton, QListWidget, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt
class SecondWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(SecondWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("App 2")
label = QLabel("INSERTED VALUES")
label.setAlignment(Qt.AlignCenter)
self.setCentralWidget(label)
#Open Database
self.conn = sqlite3.connect("connections.db")
self.cursor = self.conn.cursor()
#Add New Table To Database
self.cursor.execute('''CREATE TABLE `New`
(name text, GD text)''')
self.conn.commit()
self.conn.close()
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("App")
self.list = QListWidget()
self.cursorCount = 0
#Open Database
self.conn = sqlite3.connect("connections.db")
self.cursor = self.conn.cursor()
try:
#Try To Create A Table If It Doesn't exit
self.cursor.execute('''CREATE TABLE `Default`
(name text, GD text)''')
except:
pass
#Get A List Of All The Tables
self.cursor.execute('SELECT name FROM sqlite_master WHERE type= "table"')
for table in self.cursor.fetchall():
self.list.insertItem(self.cursorCount, table[0])
self.cursorCount += 1
self.conn.commit()
self.conn.close()
self.list.item(0).setSelected(True)
self.btn = QPushButton()
self.btn.setText("click me!")
self.btn.clicked.connect(self.openWin)
winWidget = QWidget()
self.setCentralWidget(winWidget)
layout = QVBoxLayout()
layout.addWidget(self.list)
layout.addWidget(self.btn)
winWidget.setLayout(layout)
def openWin(self):
self.win = SecondWindow()
self.win.show()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
任何帮助将不胜感激
【问题讨论】:
-
为什么你需要使用
show()而不是exec_()? -
上面的代码是简单的程序,做基础。一旦完成。第二个窗口实际上将包含更多功能。因此,以后,我需要它是
.show() -
然后在辅助窗口中创建一个自定义信号,从主窗口连接它并在需要时发出信号。
-
我该怎么做。我以前从未使用过信号。
标签: python python-3.x qt pyqt pyqt5