Calla function from another file
-
I am creating a gui based fantasy game using python and pyqt5.
I'm done with 95 % work and getting stuck while calling a function from another file.
While creating my team when I double click on a players name it gets selected in my team but if I single click on a player's name I want it to open a new gui window which shows the stats of the selected player. For this purpose I have created a new file with the gui design statements and the function to work just the way I want. When I run this file it works properly but I am unable to call it in my actual program. The program just quits when I single click on a players name without the second gui window opening
In the first image Showstats is the function where I'm calling the function from another file and I am facing problem in that part.
Second and third images are the screenshots of another file. -
Hi and welcome to devnet,
Please post your code as text using coding tags (the </> button does that for you).
One issue I can see is that you overwrote show which is a QWidget member function. This is a bad idea without calling the base class version.
-
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'page1.ui' # # Created by: PyQt5 UI code generator 5.15.4 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 600) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.Selection_box = QtWidgets.QListWidget(self.centralwidget) self.Selection_box.setGeometry(QtCore.QRect(50, 220, 251, 331)) self.Selection_box.setObjectName("Selection_box") self.Selected_box = QtWidgets.QListWidget(self.centralwidget) self.Selected_box.setGeometry(QtCore.QRect(500, 220, 251, 331)) self.Selected_box.setObjectName("Selected_box") self.Credits_a = QtWidgets.QLineEdit(self.centralwidget) self.Credits_a.setGeometry(QtCore.QRect(150, 140, 41, 22)) self.Credits_a.setObjectName("Credits_a") self.Team_Name = QtWidgets.QLabel(self.centralwidget) self.Team_Name.setGeometry(QtCore.QRect(510, 190, 71, 16)) self.Team_Name.setObjectName("Team_Name") self.Name_display = QtWidgets.QLineEdit(self.centralwidget) self.Name_display.setGeometry(QtCore.QRect(590, 190, 113, 22)) self.Name_display.setObjectName("Name_display") self.Points_Available = QtWidgets.QLabel(self.centralwidget) self.Points_Available.setGeometry(QtCore.QRect(60, 140, 91, 16)) self.Points_Available.setObjectName("Points_Available") self.Your_Selection = QtWidgets.QLabel(self.centralwidget) self.Your_Selection.setGeometry(QtCore.QRect(60, 20, 81, 16)) self.Your_Selection.setObjectName("Your_Selection") self.Credits_u = QtWidgets.QLineEdit(self.centralwidget) self.Credits_u.setGeometry(QtCore.QRect(580, 140, 41, 22)) self.Credits_u.setObjectName("Credits_u") self.Points_used = QtWidgets.QLabel(self.centralwidget) self.Points_used.setGeometry(QtCore.QRect(510, 140, 71, 16)) self.Points_used.setObjectName("Points_used") self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget) self.horizontalLayoutWidget.setGeometry(QtCore.QRect(50, 40, 711, 80)) self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget") self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget) self.horizontalLayout.setContentsMargins(0, 0, 0, 0) self.horizontalLayout.setObjectName("horizontalLayout") self.Wk_label = QtWidgets.QLabel(self.horizontalLayoutWidget) self.Wk_label.setObjectName("Wk_label") self.horizontalLayout.addWidget(self.Wk_label) self.Wk_le = QtWidgets.QLineEdit(self.horizontalLayoutWidget) self.Wk_le.setEnabled(True) self.Wk_le.setObjectName("Wk_le") self.horizontalLayout.addWidget(self.Wk_le) self.Bat_label = QtWidgets.QLabel(self.horizontalLayoutWidget) self.Bat_label.setObjectName("Bat_label") self.horizontalLayout.addWidget(self.Bat_label) self.Bat_le = QtWidgets.QLineEdit(self.horizontalLayoutWidget) self.Bat_le.setEnabled(True) self.Bat_le.setObjectName("Bat_le") self.horizontalLayout.addWidget(self.Bat_le) self.Ar_label = QtWidgets.QLabel(self.horizontalLayoutWidget) self.Ar_label.setObjectName("Ar_label") self.horizontalLayout.addWidget(self.Ar_label) self.Ar_le = QtWidgets.QLineEdit(self.horizontalLayoutWidget) self.Ar_le.setEnabled(True) self.Ar_le.setObjectName("Ar_le") self.horizontalLayout.addWidget(self.Ar_le) self.Bowl_label = QtWidgets.QLabel(self.horizontalLayoutWidget) self.Bowl_label.setObjectName("Bowl_label") self.horizontalLayout.addWidget(self.Bowl_label) self.Bowl_le = QtWidgets.QLineEdit(self.horizontalLayoutWidget) self.Bowl_le.setEnabled(True) self.Bowl_le.setObjectName("Bowl_le") self.horizontalLayout.addWidget(self.Bowl_le) self.radiobutton_5 = QtWidgets.QRadioButton(self.centralwidget) self.radiobutton_5.setGeometry(QtCore.QRect(184, 200, 57, 20)) self.radiobutton_5.setObjectName("radiobutton_5") self.radiobutton_6 = QtWidgets.QRadioButton(self.centralwidget) self.radiobutton_6.setGeometry(QtCore.QRect(56, 200, 58, 20)) self.radiobutton_6.setObjectName("radiobutton_6") self.radiobutton_7 = QtWidgets.QRadioButton(self.centralwidget) self.radiobutton_7.setGeometry(QtCore.QRect(247, 200, 58, 20)) self.radiobutton_7.setObjectName("radiobutton_7") self.radiobutton_8 = QtWidgets.QRadioButton(self.centralwidget) self.radiobutton_8.setGeometry(QtCore.QRect(120, 200, 58, 20)) self.radiobutton_8.setObjectName("radiobutton_8") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22)) self.menubar.setObjectName("menubar") self.menuFile = QtWidgets.QMenu(self.menubar) self.menuFile.setObjectName("menuFile") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.actionNew_Team = QtWidgets.QAction(MainWindow) self.actionNew_Team.setObjectName("actionNew_Team") self.actionSave_Team = QtWidgets.QAction(MainWindow) self.actionSave_Team.setObjectName("actionSave_Team") self.actionEvaluate_Team = QtWidgets.QAction(MainWindow) self.actionEvaluate_Team.setObjectName("actionEvaluate_Team") self.actionOpen_Team = QtWidgets.QAction(MainWindow) self.actionOpen_Team.setObjectName("actionOpen_Team") self.menuFile.addAction(self.actionNew_Team) self.menuFile.addAction(self.actionSave_Team) self.menuFile.addAction(self.actionEvaluate_Team) self.menuFile.addAction(self.actionOpen_Team) self.menubar.addAction(self.menuFile.menuAction()) self.Selection_box.itemDoubleClicked.connect(self.removelist1) self.Selection_box.itemClicked.connect(self.showstats) self.Selected_box.itemDoubleClicked.connect(self.removelist2) self.radiobutton_5.toggled.connect(self.ctg) self.radiobutton_6.toggled.connect(self.ctg) self.radiobutton_7.toggled.connect(self.ctg) self.radiobutton_8.toggled.connect(self.ctg) self.menuFile.triggered[QtWidgets.QAction].connect(self.menu) self.bat=0 self.bowl=0 self.ar=0 self.wk=0 self.avl=1100 self.used=0 self.Selection_box.clear() self.Selected_box.clear() self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.Team_Name.setText(_translate("MainWindow", "Team Name:")) self.Points_Available.setText(_translate("MainWindow", "Points Available:")) self.Your_Selection.setText(_translate("MainWindow", "Your Selection")) self.Points_used.setText(_translate("MainWindow", "Points Used:")) self.Wk_label.setText(_translate("MainWindow", "Wk :")) self.Bat_label.setText(_translate("MainWindow", "Bat : ")) self.Ar_label.setText(_translate("MainWindow", "Ar :")) self.Bowl_label.setText(_translate("MainWindow", "Bowl :")) self.radiobutton_5.setText(_translate("MainWindow", "Ar")) self.radiobutton_6.setText(_translate("MainWindow", "Wk")) self.radiobutton_7.setText(_translate("MainWindow", "Bowl")) self.radiobutton_8.setText(_translate("MainWindow", "Bat")) self.menuFile.setTitle(_translate("MainWindow", "File")) self.actionNew_Team.setText(_translate("MainWindow", "New Team")) self.actionSave_Team.setText(_translate("MainWindow", "Save Team")) self.actionEvaluate_Team.setText(_translate("MainWindow", "Evaluate Team")) self.actionOpen_Team.setText(_translate("MainWindow", "Open Team")) def menu(self,action): txt=(action.text()) print("q") if txt == 'New Team': print("s") self.bat=0 self.bowl=0 self.ar=0 self.wk=0 self.avl=1100 self.used=0 self.Selection_box.clear() self.Selected_box.clear() text, ok=QtWidgets.QInputDialog.getText(MainWindow, "Team", "Enter name of team:") if ok: self.Name_display.setText(str(text)) self.show() if txt=='Save Team': print('b') count=self.Selected_box.count() selected="" for i in range(count): selected+=self.Selected_box.item(i).text() if i<count: selected+="," self.saveteam(self.Name_display.text(),selected,self.used) if txt == 'Open Team': self.bat=0 self.bowl=0 self.ar=0 self.wk=0 self.avl=1100 self.used=0 self.Selection_box.clear() self.Selected_box.clear() self.show() self.openteam() if txt == 'Evaluate Team': from dlgscore import Ui_Dialog Dialog = QtWidgets.QDialog() ui = Ui_Dialog() ui.setupUi(Dialog) ret = Dialog.exec() def show(self): self.Bat_le.setText(str(self.bat)) self.Bowl_le.setText(str(self.bowl)) self.Wk_le.setText(str(self.wk)) self.Ar_le.setText(str(self.ar)) self.Credits_a.setText(str(self.avl)) self.Credits_u.setText(str(self.used)) def showstats(self,item): from Stats import Ui_MainWindow print('a') MainWindow = QtWidgets.QMainwindow() ui = Ui_MainWindow() print('f') ui.setupUi(MainWindow) ret = MainWindow.exec() print("a") def criteria(self,ctgr,item): msg='' if ctgr=="Bat" and self.bat>=5:msg="Batsmen not more than 5" if ctgr=="Bowl" and self.bowl>=5:msg="Bowlers not more than 5" if ctgr=="AR" and self.ar>=3:msg="Allrounders not more than 3" if ctgr=="Wk" and self.wk>=1:msg="Wicketkeepers not more than 1" if msg!='': #msg="You Have Exhausted your Points" self.showdlg(msg) return False if self.avl<=0: msg="You Have Exhausted your Points" self.showdlg(msg) return False if ctgr=="Bat":self.bat+=1 if ctgr=="Bowl":self.bowl+=1 if ctgr=="AR":self.ar+=1 if ctgr=="Wk":self.wk+=1 sql="SELECT Value from Stats where Player='"+item.text()+"'" cur=conn.execute(sql) row=cur.fetchone() self.avl-=int(row[0]) self.used+=int(row[0]) return True def removelist1(self,item): ctgr='' if self.radiobutton_8.isChecked()==True: ctgr='Bat' if self.radiobutton_7.isChecked()==True: ctgr='Bowl' if self.radiobutton_5.isChecked()==True: ctgr='AR' if self.radiobutton_6.isChecked()==True: ctgr='Wk' ret = self.criteria(ctgr,item) if ret==True: self.Selection_box.takeItem(self.Selection_box.row(item)) self.Selected_box.addItem(item.text()) self.show() def ctg(self): ctgr='' if self.radiobutton_8.isChecked()==True: ctgr='Bat' elif self.radiobutton_7.isChecked()==True: ctgr='Bowl' elif self.radiobutton_5.isChecked()==True: ctgr='AR' elif self.radiobutton_6.isChecked()==True: ctgr='Wk' self.fillList(ctgr) def removelist2(self,item): self.Selected_box.takeItem(self.Selected_box.row(item)) curs = conn.execute("SELECT Player,Value,Category from Stats where Player='"+item.text()+"'") row=curs.fetchone() self.avl=self.avl+int(row[1]) self.used=self.used-int(row[1]) ctgr=row[2] if ctgr=="Bat": self.bat-=1 if self.radiobutton_8.isChecked()==True:self.Selection_box.addItem(item.text()) if ctgr=="Bowl": self.bowl-=1 if self.radiobutton_7.isChecked()==True:self.Selection_box.addItem(item.text()) if ctgr=="AR": self.ar-=1 if self.radiobutton_5.isChecked()==True:self.Selection_box.addItem(item.text()) if ctgr=="Wk": self.wk-=1 if self.radiobutton_6.isChecked()==True:self.Selection_box.addItem(item.text()) self.show() def fillList(self,ctgr): if self.Name_display.text()=='': self.showdlg("Enter name of team") return self.Selection_box.clear() sql="SELECT player from Stats where category='"+ctgr+"';" cur=conn.execute(sql) for row in cur: selected=[] for i in range(self.Selected_box.count()): selected.append(self.Selected_box.item(i).text()) if row[0] not in selected:self.Selection_box.addItem(row[0]) def openteam(self): sql="select Name from Teams;" cur = conn.execute(sql) teams=[] for row in cur: teams.append(row[0]) team, ok=QtWidgets.QInputDialog.getItem(MainWindow,"Dream","Choose A Team",teams,0,False) if ok and team: self.Name_display.setText(team) sql1="SELECT Players,Value from Teams where Name='"+team+"';" cur = conn.execute(sql1) row=cur.fetchone() selected=row[0].split(',') self.Selected_box.addItems(selected) self.used=row[1] self.avl=1000-row[1] count=self.Selected_box.count() ##iterate to count no. of specific players for i in range(count-1): player=self.Selected_box.item(i).text() sql="SELECT Category from Stats where Player='"+player+"';" curs = conn.execute(sql) row=curs.fetchone() ctgr=row[0] if ctgr=="Bat":self.bat+=1 if ctgr=="Bowl":self.bowl+=1 if ctgr=="Ar":self.ar+=1 if ctgr=="Wk":self.wk+=1 self.show() def saveteam(self,Name,player,Value): if self.bat+self.bowl+self.ar+self.wk!=11: self.showdlg("Insufficient players") return sql="INSERT INTO Teams (Name,Players,Value) VALUES ('"+Name+"','"+player+"','"+str(Value)+"');" try: cur = conn.execute(sql) self.showdlg("Team Saved Succesfully") conn.commit() except: self.showdlg("Error in Operation") conn.rollback() def showdlg(self,msg): Dialog=QtWidgets.QMessageBox() Dialog.setText(msg) Dialog.setWindowTitle("Dream Team selector") ret=Dialog.exec() if __name__ == "__main__": import sys import sqlite3 conn = sqlite3.connect('Fpx_11.db') app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
-
Another thing that looks wrong: you are calling exec on a QMainWindow instance. The only widgets that have that method are those coming from QDialog.
-
Rename show to something more meaningful.
Use QDialog as base class for your base class.One other note: did you notice the warning in the header of your Python script ?
-
@SGaist Thank You so much for your help and time. My problem got solved. As you suggested, I used QDialog as a base class for my other script and everything worked out fine. After that I faced some minor issues but they were solved and now my program runs exactly the way I want.