Issue
I've spent a lot of time making GUIs using tkinter, but never used the OOP style so I wanted to start learning PyQt5 and in turn start learning OOP (definitely struggling through it).
I currently have three files:
app.py - "main entry point" for the application
main_menu.py - Python file converted from the PyQt designer .ui file
add_functions.py - file that will have functions to update GUI based on user actions and run functions from button clicks
My question is, how do I update the initial values of the combo boxes, outside of the "main_menu.py" file where the widgets are created? I'm assuming I do it somewhere in the "app.py" file, but I cannot figure it out.
The code below doesn't throw any errors, but the combo boxes are blank where one should be a list of strings and the other a single string (will later be updated based on first combo box selection)
I was able to get it to work with everything basically in the "main_menu.py" file, but then I realized every time I want to change the UI, it would create a problem.
app.py
from PyQt5.QtWidgets import QMainWindow
from PyQt5 import QtWidgets
# import GUI from designer file
from main_menu import Ui_main_menu
# import other functions
from add_functions import ChangeLists
class Main(QMainWindow, Ui_main_menu):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setupUi(self)
# Team List
team_list_file = open(r'C:\NHLdb_pyqt\files\NHLteams.txt', 'r')
team_list = team_list_file.read().splitlines()
team_list_file.close()
print("team list: ", team_list)
# Initial Player List
player_list_init = "Please Select a Team"
# update combo box lists
print("team list: ", team_list)
self.team_select_combobox.addItems(team_list)
self.player_select_combobox.addItem(player_list_init)
# connect combo box to function that will change player list based on team list selection
# self.team_select_combobox.currentTextChanged.connect(ChangeLists.team_changed)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main_menu = QtWidgets.QDialog()
ui = Ui_main_menu()
ui.setupUi(main_menu)
main_menu.show()
sys.exit(app.exec_())
main_menu.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_main_menu(object):
def setupUi(self, main_menu):
main_menu.setObjectName("main_menu")
main_menu.resize(463, 408)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(main_menu)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.tabWidget = QtWidgets.QTabWidget(main_menu)
self.tabWidget.setObjectName("tabWidget")
self.get_data_tab = QtWidgets.QWidget()
self.get_data_tab.setObjectName("get_data_tab")
self.verticalLayout = QtWidgets.QVBoxLayout(self.get_data_tab)
self.verticalLayout.setObjectName("verticalLayout")
self.team_player_layout = QtWidgets.QGridLayout()
self.team_player_layout.setObjectName("team_player_layout")
self.player_select_label = QtWidgets.QLabel(self.get_data_tab)
self.player_select_label.setAlignment(QtCore.Qt.AlignCenter)
self.player_select_label.setObjectName("player_select_label")
self.team_player_layout.addWidget(self.player_select_label, 2, 0, 1, 1)
self.team_select_combobox = QtWidgets.QComboBox(self.get_data_tab)
self.team_select_combobox.setCurrentText("")
self.team_select_combobox.setPlaceholderText("")
self.team_select_combobox.setObjectName("team_select_combobox")
self.team_player_layout.addWidget(self.team_select_combobox, 0, 1, 1, 1)
self.player_select_combobox = QtWidgets.QComboBox(self.get_data_tab)
self.player_select_combobox.setObjectName("player_select_combobox")
self.team_player_layout.addWidget(self.player_select_combobox, 2, 1, 1, 1)
self.team_select_label = QtWidgets.QLabel(self.get_data_tab)
self.team_select_label.setAlignment(QtCore.Qt.AlignCenter)
self.team_select_label.setObjectName("team_select_label")
self.team_player_layout.addWidget(self.team_select_label, 0, 0, 1, 1)
self.get_player_data_button = QtWidgets.QPushButton(self.get_data_tab)
self.get_player_data_button.setObjectName("get_player_data_button")
self.team_player_layout.addWidget(self.get_player_data_button, 3, 0, 1, 2)
self.verticalLayout.addLayout(self.team_player_layout)
self.tableWidget = QtWidgets.QTableWidget(self.get_data_tab)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth())
self.tableWidget.setSizePolicy(sizePolicy)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(0)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
self.verticalLayout.addWidget(self.tableWidget)
self.tabWidget.addTab(self.get_data_tab, "")
self.view_db_tab = QtWidgets.QWidget()
self.view_db_tab.setObjectName("view_db_tab")
self.tabWidget.addTab(self.view_db_tab, "")
self.horizontalLayout_2.addWidget(self.tabWidget)
self.retranslateUi(main_menu)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(main_menu)
def retranslateUi(self, main_menu):
_translate = QtCore.QCoreApplication.translate
main_menu.setWindowTitle(_translate("main_menu", "NHL Database"))
self.player_select_label.setText(_translate("main_menu", "Select A Player:"))
self.team_select_label.setText(_translate("main_menu", "Select A Team:"))
self.get_player_data_button.setText(_translate("main_menu", "Get Player Data"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("main_menu", "Year"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("main_menu", "Team"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("main_menu", "League"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("main_menu", "GP"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.get_data_tab), _translate("main_menu", "Get Player Data"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.view_db_tab), _translate("main_menu", "View Database"))
add_functions
from os import path
from PyQt5 import QtCore, QtGui, QtWidgets
class ChangeLists:
def team_changed(self):
index = self.team_select_combobox.currentText()
if index != 0 and path.exists(r'C:\NHLdb\files\_' + str(index) + '.txt'):
player_list_file = open(r'C:\NHLdb\files\_' + str(index) + '.txt', 'r')
player_list_update = player_list_file.read().splitlines()
player_list_file.close()
self.player_select_combobox.clear()
self.player_select_combobox.addItems(player_list_update)
if index != 0 and not path.exists(r'C:\NHLdb\files\_' + str(index) + '.txt'):
self.player_select_combobox.clear()
self.player_select_combobox.addItem("Prospects Not Compiled")
Solution
Your calling order in the app.py is a bit off. Class Main is never called and hence your intialization routine defined there never gets executed. Additionally, class Main inherits from the wrong Window type. In your case, you designed a QDialog, so it should inherit from that (instead of QMainWindow).
Upon fixing these issues, everything you defined in the Main class should work as expected:
modified app.py
from PyQt5 import QtWidgets
from main_menu import Ui_main_menu
class Main(QtWidgets.QDialog, Ui_main_menu):
def __init__(self):
super().__init__()
# setup ui
self.setupUi(self)
# Team List
team_list_file = open(r'D:\temp.txt', 'r')
team_list = team_list_file.read().splitlines()
team_list_file.close()
# Initial Player List
player_list_init = "Please Select a Team"
# update combo box lists
self.team_select_combobox.addItems(team_list)
self.player_select_combobox.addItem(player_list_init)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
ui = Main()
ui.show()
app.exec_()
Answered By - Christian Karcher
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.