Issue
I am using PyQt5, where I am trying to create teams and work on a league system.
I have created Action Buttons which open Dialog boxes. I want to populate certain lists from the database based on a team name I choose from my dialog window.
I think I am stuck, because I cannot understand how to communicate between the two.
When I try to add a new team, I want that all lists in my main window get appropriately filled. But how do I pass this information from the dialog box to the main window and also close the dialog box immediately after that?
Here is the code:
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()
LINK: https://www.paste.org/99817
Solution
Is this what you are looking for?
You are creating the OpenTeamDialog
and NewTeamDialog
classes through methods, but the dialogs don't know anything about the Window
class so you must pass it as a parameter when initializing them so that you can access all of its widgets.
This is assuming that your databse is the same meaning there are the following tables: ALLROUNDERS, BATSMEN,BOWLERS, WICKETKEEPER and a field column called TEAM:
Also I am setting up the UI in another class so I would delete any thing besides the UI part that you got from QtDesigner in the Ui_MainWindow class.
There is most likely a better way to implement this but this is just a crude design based on your needs.
class Window(QtWidgets.QMainWindow,Ui_MainWindow):
def __init__(self, parent = None):
super().__init__(parent)
self.setupUi(self)
self.setupEvents()
def setupEvents(self):
self.actionNew_Team.triggered.connect(self.newTeam)
self.actionOpen_Team.triggered.connect(self.openTeam)
def newTeam(self):
n = NewTeamDialog(self)
n.exec_()
def openTeam(self):
o = OpenTeamDialog(self)
o.exec_()
def saveTeam(self):
pass
class NewTeamDialog(QtWidgets.QDialog):
def __init__(self,window,parent = None):
super(NewTeamDialog, self).__init__(parent)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.window = window
self.setWindowTitle("Create New Team")
self.setFixedWidth(300)
self.setFixedHeight(100)
self.dropDown_TeamType = QtWidgets.QComboBox()
#Added dropdown to choose which team goes in the team type
self.dropDown_TeamType.addItems(["ALLROUNDERS", "BATSMEN", "BOWLERS","WICKETKEEPER" ])
self.nameInput = QtWidgets.QLineEdit()
self.nameInput.setPlaceholderText("Enter Team Name")
self.addBtn = QtWidgets.QPushButton()
self.addBtn.setText("Add Team")
self.addBtn.clicked.connect(self.addTeam)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.dropDown_TeamType)
layout.addWidget(self.nameInput)
layout.addWidget(self.addBtn)
self.setLayout(layout)
def addTeam(self):
name = self.nameInput.text()
team_type = self.dropDown_TeamType.currentText()
conn = sqlite3.connect("example.db")
c = conn.cursor()
#adds team to the database using the current selected dropdown item
c.execute("SELECT TEAM FROM {0} WHERE TEAM=?;".format(team_type),(name.title(),))
exists = c.fetchall()
if not exists:
c.execute("INSERT INTO {0} VALUES('{1}');".format(team_type,name.title()))
conn.commit()
conn.close()
conn.close()
self.close()
if team_type == "BATSMEN":
item = QtWidgets.QListWidgetItem(name)
self.window.listWidget_3.addItem(item)
elif team_type == "ALLROUNDERS":
item = QtWidgets.QListWidgetItem(name)
self.window.listWidget_4.addItem(item)
elif team_type == "BOWLERS":
item = QtWidgets.QListWidgetItem(name)
self.window.listWidget_5.addItem(item)
elif team_type == "WICKETKEEPER":
item = QtWidgets.QListWidgetItem(name)
self.window.listWidget_6.addItem(item)
class OpenTeamDialog(QtWidgets.QDialog):
def __init__(self, window, parent = None):
super(OpenTeamDialog, self).__init__(parent)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.window = window
self.setWindowTitle("Open Saved Team")
self.setFixedWidth(300)
self.setFixedHeight(100)
self.dropDown_TeamType = QtWidgets.QComboBox()
self.dropDown_TeamType.addItems(["ALLROUNDERS", "BATSMEN", "BOWLERS","WICKETKEEPER" ])
self.dropDown = QtWidgets.QComboBox()
self.dropDown_TeamType.currentIndexChanged.connect(self.itemChanged)
self.addBtn = QtWidgets.QPushButton()
self.addBtn.setText("Add Team")
self.addBtn.clicked.connect(self.openTeam)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.dropDown_TeamType)
layout.addWidget(self.dropDown)
layout.addWidget(self.addBtn)
self.setLayout(layout)
conn = sqlite3.connect("example.db")
conn.row_factory = lambda cursor, row: row[0]
c = conn.cursor()
c.execute("SELECT TEAM FROM ALLROUNDERS")
result = c.fetchall()
self.dropDown.addItems(result)
def itemChanged(self):
#adds all items from the database 'Team' column to drop down whenever it changes
team_type = self.dropDown_TeamType.currentText()
self.dropDown.clear()
conn = sqlite3.connect("example.db")
conn.row_factory = lambda cursor, row: row[0]
c = conn.cursor()
c.execute("SELECT TEAM FROM {0}".format(team_type))
result = c.fetchall()
self.dropDown.addItems(result)
conn.close()
def openTeam(self):
team_type = self.dropDown_TeamType.currentText()
team_name = self.dropDown.currentText()
self.close()
if team_type == "BATSMEN":
item = QtWidgets.QListWidgetItem(team_name)
self.window.listWidget_3.addItem(item)
elif team_type == "ALLROUNDERS":
item = QtWidgets.QListWidgetItem(team_name)
self.window.listWidget_4.addItem(item)
elif team_type == "BOWLERS":
item = QtWidgets.QListWidgetItem(team_name)
self.window.listWidget_5.addItem(item)
elif team_type == "WICKETKEEPER":
item = QtWidgets.QListWidgetItem(team_name)
self.window.listWidget_6.addItem(item)
class EvaluateDialog(QtWidgets.QDialog):
pass
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = Window()
MainWindow.show()
sys.exit(app.exec_())
Here is a .py file you can use to create the same database:
import sqlite3
def createTables():
connection = sqlite3.connect("example.db")
connection.execute("CREATE TABLE ALLROUNDERS(TEAM TEXT NOT NULL)")
connection.execute("CREATE TABLE BATSMEN(TEAM TEXT NOT NULL)")
connection.execute("CREATE TABLE BOWLERS(TEAM TEXT NOT NULL)")
connection.execute("CREATE TABLE WICKETKEEPER(TEAM TEXT NOT NULL)")
connection.execute("INSERT INTO ALLROUNDERS VALUES(?)",('Empty',))
connection.execute("INSERT INTO BATSMEN VALUES(?)",('Empty',))
connection.execute("INSERT INTO BOWLERS VALUES(?)",('Empty',))
connection.execute("INSERT INTO WICKETKEEPER VALUES(?)",('Empty',))
connection.commit()
result = connection.execute("SELECT * FROM BATSMEN")
connection.close()
createTables()
Answered By - Drees
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.