我正在使用PyQt5,在那里我尝试创建团队,并在联赛系统上工作。你知道吗
我已经创建了打开对话框的操作按钮。 我想根据从对话框窗口中选择的团队名称填充数据库中的某些列表。你知道吗
我想我被困住了,因为我不明白如何在两者之间沟通。你知道吗
当我尝试添加新团队时,我希望主窗口中的所有列表都得到适当的填充。但如何将此信息从对话框传递到主窗口,并在之后立即关闭对话框?你知道吗
代码如下:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QInputDialog, QLineEdit, QDialog, QWidget, QPushButton, QHBoxLayout, QVBoxLayout, QApplication, QComboBox
import sqlite3
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
#removed because too big
#full code in link
def setupEvents(self, MainWindow):
self.actionNew_Team.triggered.connect(self.newTeam)
self.actionOpen_Team.triggered.connect(self.openTeam)
def newTeam(self, MainWindow):
n = NewTeamDialog()
n.exec_()
def openTeam(self, MainWindow):
o = OpenTeamDialog()
o.exec_()
def saveTeam(self, MainWindow):
pass
class NewTeamDialog(QDialog):
def __init__(self):
super(NewTeamDialog, self).__init__()
self.setWindowTitle("Create New Team")
self.setFixedWidth(300)
self.setFixedHeight(100)
self.nameInput = QLineEdit()
self.nameInput.setPlaceholderText("Enter Team Name")
self.addBtn = QPushButton()
self.addBtn.setText("Add Team")
self.addBtn.clicked.connect(self.addTeam)
layout = QVBoxLayout()
layout.addWidget(self.nameInput)
layout.addWidget(self.addBtn)
self.setLayout(layout)
def addTeam(self):
name = self.nameInput.text()
conn = sqlite3.connect("example.db")
c = conn.cursor()
c.execute('SELECT * FROM batsmen')
print(c.fetchall())
conn.close()
self.close()
这就是你要找的吗?你知道吗
您正在通过方法创建
OpenTeamDialog
和NewTeamDialog
类,但是对话框对Window
类一无所知,因此在初始化它们时必须将其作为参数传递,以便可以访问它的所有小部件。你知道吗这是假设您的数据库是相同的,即有以下表格: 全能选手、击球手、保龄球手、守门员和一个名为“团队”的栏位:
另外,我正在另一个类中设置UI,所以除了UI\u MainWindow类中的QtDesigner提供的UI部分之外,我会删除任何东西。你知道吗
很可能有更好的方法来实现这一点,但这只是一个基于您的需求的粗略设计。你知道吗
下面是一个.py文件,可用于创建相同的数据库:
相关问题 更多 >
编程相关推荐