在PyQt5中,我有一个窗口,它从数据库中读取信息并将这些信息添加到一个widget中。还有一个窗口,当它打开时,向数据库添加一个新的表。我想做的是在第二个窗口关闭时重新加载主窗口。我不想把二级窗口改为
QDialoge
,需要使用
.show()
,而不是
.exec_()
。我有什么办法可以做到这一点吗。
There is my code:
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 To Create A Table If It Doesn't exit
self.cursor.execute('''CREATE TABLE `Default`
(name text, GD text)''')
except:
#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_()
希望得到任何帮助