Select query is not executing in python3-sqlite3, instead my program breaks right after that statement. My code is running fine with no errors but whenever i click on radio button (in GUI) which calls a function containing above execute statement ,it breaks.
I'm new to sqlite3 and even python3, I'm working on my first python project which is built using above 2 and Qt designer, after converting my code from qtdesigner i added necessary codes, here's MyCricket.py code:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import (QApplication, QWidget, QLabel, QPushButton, QRadioButton, QFrame, QMessageBox)
from DialogBox2 import Ui_Dialog
from PyQt5.QtCore import pyqtSlot, QRect
class Ui_MainWindow(object):
def setupUi(self,MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(730, 562)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 30, 101, 21))
font = QtGui.QFont()
font.setPointSize(9)
self.label.setFont(font)
self.label.setObjectName("label")
self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(20, 60, 681, 61))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_8 = QtWidgets.QLabel(self.horizontalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.label_8.setFont(font)
self.label_8.setObjectName("label_8")
self.horizontalLayout.addWidget(self.label_8)
self.l1 = QtWidgets.QLabel(self.horizontalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.l1.setFont(font)
self.l1.setObjectName("l1")
self.horizontalLayout.addWidget(self.l1)
self.label_9 = QtWidgets.QLabel(self.horizontalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.label_9.setFont(font)
self.label_9.setObjectName("label_9")
self.horizontalLayout.addWidget(self.label_9)
self.l2 = QtWidgets.QLabel(self.horizontalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.l2.setFont(font)
self.l2.setObjectName("l2")
self.horizontalLayout.addWidget(self.l2)
self.label_5 = QtWidgets.QLabel(self.horizontalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.horizontalLayout.addWidget(self.label_5)
self.l3 = QtWidgets.QLabel(self.horizontalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.l3.setFont(font)
self.l3.setObjectName("l3")
self.horizontalLayout.addWidget(self.l3)
self.label_3 = QtWidgets.QLabel(self.horizontalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.horizontalLayout.addWidget(self.label_3)
self.l4 = QtWidgets.QLabel(self.horizontalLayoutWidget)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.l4.setFont(font)
self.l4.setObjectName("l4")
self.horizontalLayout.addWidget(self.l4)
self.horizontalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(20, 130, 681, 51))
self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_13 = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(8)
font.setBold(True)
font.setWeight(75)
self.label_13.setFont(font)
self.label_13.setObjectName("label_13")
self.horizontalLayout_2.addWidget(self.label_13)
self.l5 = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.l5.setFont(font)
self.l5.setObjectName("l5")
self.horizontalLayout_2.addWidget(self.l5)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem)
self.label_11 = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(8)
font.setBold(True)
font.setWeight(75)
self.label_11.setFont(font)
self.label_11.setObjectName("label_11")
self.horizontalLayout_2.addWidget(self.label_11)
self.l6 = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.l6.setFont(font)
self.l6.setObjectName("l6")
self.horizontalLayout_2.addWidget(self.l6)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem1)
self.listW1 = QtWidgets.QListWidget(self.centralwidget)
self.listW1.setGeometry(QtCore.QRect(30, 230, 261, 271))
font = QtGui.QFont()
font.setFamily("Arial")
font.setBold(True)
font.setWeight(75)
self.listW1.setFont(font)
self.listW1.setObjectName("listW1")
item = QtWidgets.QListWidgetItem()
self.listW2 = QtWidgets.QListWidget(self.centralwidget)
self.listW2.setGeometry(QtCore.QRect(350, 230, 281, 271))
font = QtGui.QFont()
font.setFamily("Arial")
font.setBold(True)
font.setWeight(75)
self.listW2.setFont(font)
self.listW2.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.listW2.setObjectName("listW2")
self.horizontalLayoutWidget_3 = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget_3.setGeometry(QtCore.QRect(30, 190, 261, 51))
self.horizontalLayoutWidget_3.setObjectName("horizontalLayoutWidget_3")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_3)
self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.rb1 = QtWidgets.QRadioButton(self.horizontalLayoutWidget_3)
self.rb1.setObjectName("rb1")
self.horizontalLayout_3.addWidget(self.rb1)
self.rb2 = QtWidgets.QRadioButton(self.horizontalLayoutWidget_3)
self.rb2.setObjectName("rb2")
self.horizontalLayout_3.addWidget(self.rb2)
self.rb3 = QtWidgets.QRadioButton(self.horizontalLayoutWidget_3)
self.rb3.setObjectName("rb3")
self.horizontalLayout_3.addWidget(self.rb3)
self.rb4 = QtWidgets.QRadioButton(self.horizontalLayoutWidget_3)
self.rb4.setObjectName("rb4")
self.horizontalLayout_3.addWidget(self.rb4)
self.horizontalLayoutWidget_4 = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget_4.setGeometry(QtCore.QRect(350, 190, 281, 51))
self.horizontalLayoutWidget_4.setObjectName("horizontalLayoutWidget_4")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_4)
self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_4.addItem(spacerItem2)
self.label_14 = QtWidgets.QLabel(self.horizontalLayoutWidget_4)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_14.setFont(font)
self.label_14.setObjectName("label_14")
self.horizontalLayout_4.addWidget(self.label_14)
self.l7 = QtWidgets.QLabel(self.horizontalLayoutWidget_4)
self.l7.setObjectName("l7")
self.horizontalLayout_4.addWidget(self.l7)
spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_4.addItem(spacerItem3)
self.label_16 = QtWidgets.QLabel(self.centralwidget)
self.label_16.setGeometry(QtCore.QRect(310, 320, 21, 41))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(False)
font.setWeight(50)
self.label_16.setFont(font)
self.label_16.setObjectName("label_16")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 730, 21))
self.menubar.setObjectName("menubar")
self.menuManage_Teams = QtWidgets.QMenu(self.menubar)
self.menuManage_Teams.setObjectName("menuManage_Teams")
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.actionOPEN_Team = QtWidgets.QAction(MainWindow)
self.actionOPEN_Team.setObjectName("actionOPEN_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.actionEVALUATE_Team.triggered.connect(self.OpenWindow)
self.menuManage_Teams.addAction(self.actionNEW_Team)
self.menuManage_Teams.addSeparator()
self.menuManage_Teams.addAction(self.actionOPEN_Team)
self.menuManage_Teams.addSeparator()
self.menuManage_Teams.addAction(self.actionSAVE_Team)
self.menuManage_Teams.addSeparator()
self.menuManage_Teams.addAction(self.actionEVALUATE_Team)
self.menubar.addAction(self.menuManage_Teams.menuAction())
#self.listW1.itemDoubleClicked.connect(self.removelist1)
#self.listW2.itemDoubleClicked.connect(self.removelist2)
self.rb1.clicked.connect(self.fillList)
self.rb2.clicked.connect(self.fillList)
self.rb3.clicked.connect(self.fillList)
self.rb4.clicked.connect(self.fillList)
#self.menuManage_Teams.triggered[QtWidgets.QAction].connect(self.menu)
self.bat=0
self.bwl=0
self.ar=0
self.wk=0
self.avl=1000
self.used=0
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Fantasy Cricket Game"))
self.label.setText(_translate("MainWindow", "Your Selections"))
self.label_8.setText(_translate("MainWindow", "Batsmen(BAT)"))
self.l1.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" color:#00aa00;\">##</span></p></body></html>"))
self.label_9.setText(_translate("MainWindow", "Bowlers(BOW)"))
self.l2.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" color:#00aa00;\">##</span></p></body></html>"))
self.label_5.setText(_translate("MainWindow", "Allrounders(AR)"))
self.l3.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" color:#00aa00;\">##</span></p></body></html>"))
self.label_3.setText(_translate("MainWindow", "Wicket-keeper(WK)"))
self.l4.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" color:#00aa00;\">##</span></p></body></html>"))
self.label_13.setText(_translate("MainWindow", "Points Available"))
self.l5.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" color:#00aa00;\">####</span></p></body></html>"))
self.label_11.setText(_translate("MainWindow", "Points Used"))
self.l6.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" color:#00aa00;\">####</span></p></body></html>"))
self.rb1.setText(_translate("MainWindow", "BAT"))
self.rb2.setText(_translate("MainWindow", "BOW"))
self.rb3.setText(_translate("MainWindow", "AR"))
self.rb4.setText(_translate("MainWindow", "WK"))
self.label_14.setText(_translate("MainWindow", "Team Name"))
self.l7.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" color:#00aa00;\">Displayed Here</span></p></body></html>"))
self.label_16.setText(_translate("MainWindow", ">"))
self.menuManage_Teams.setTitle(_translate("MainWindow", "Manage Teams"))
self.actionNEW_Team.setText(_translate("MainWindow", "NEW Team"))
self.actionOPEN_Team.setText(_translate("MainWindow", "OPEN Team"))
self.actionSAVE_Team.setText(_translate("MainWindow", "SAVE Team"))
self.actionEVALUATE_Team.setText(_translate("MainWindow", "EVALUATE Team"))
def fillList(self):
curcricket = MyCricket.cursor()
if self.rb1.isChecked(): # Only showing for radiobutton1 code for rest buttons are similar.
print("HELP")
curcricket.execute("SELECT * FROM Stats WHERE Ctg = BAT;") # This is breaking my code, i added 2 print 'help' to check this breaking.
print("HELP")
BT = curcricket.fetchall()
self.listW1.clear()
for i in range(len(BT)):
item1 = QtWidgets.QListWidgetItem(BT[i][0])
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
item1.setFont(font)
self.listW1.addItem(item1)
curcricket.close()
if __name__ == "__main__":
import sqlite3
MyCricket=sqlite3.connect('MyCricket.db')
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
My application contains Qradiobuttons(BAT,BWL,AR,WK) and QlistWidgets
Whenever radio buttons are clicked i want my list widget to be filled with my Stats Table (only column 1 i.e "Player"(player-name) of particular Category column 7 i.e "Ctg" (BAT,BWL..) ) of database MyCricket.db. Im stuck with this problem and im not able to proceed futher. I wasted a lot of time searching over internet but didnt find a soln to this,I'm still researching so if anyone has the answer, please let me know.
Related
I have a GUI made by PyQt5. On this GUI, there is a push button and a frame which includes several radio buttons as illustrated below:
I want to change the (enabled/disabled) states of the radio buttons in the frame every time I click the push button. Since the frame is the parent of the radio buttons, I think this could be achieved by disabling or enabling the frame when the push button is clicked. Below is the code in my m_view.py module and my main_window.py module:
m_view.py:
from PyQt5 import QtWidgets
import sys
from main_window import Ui_Form
class MyMainWindow(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent = None):
super(MyMainWindow, self).__init__(parent)
self.setupUi(self)
self.destroyed.connect(sys.exit)
self.pushButtonOK.clicked.connect(self._update_status_of_frame)
def _update_status_of_frame(self):
status = self.frame.isEnabled()
self.frame.setEnabled(not status)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyMainWindow()
window.show()
sys.exit(app.exec_())
main_window.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main_window.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# 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_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(176, 264)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/icons/ORing_icon.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Form.setWindowIcon(icon)
self.gridLayout = QtWidgets.QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.labelForItems = QtWidgets.QLabel(Form)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.labelForItems.sizePolicy().hasHeightForWidth())
self.labelForItems.setSizePolicy(sizePolicy)
self.labelForItems.setMinimumSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(12)
font.setBold(False)
font.setWeight(50)
self.labelForItems.setFont(font)
self.labelForItems.setStyleSheet("")
self.labelForItems.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.labelForItems.setObjectName("labelForItems")
self.gridLayout.addWidget(self.labelForItems, 0, 0, 1, 2)
self.frame = QtWidgets.QFrame(Form)
self.frame.setEnabled(False)
self.frame.setFrameShape(QtWidgets.QFrame.Panel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.verticalLayout = QtWidgets.QVBoxLayout(self.frame)
self.verticalLayout.setObjectName("verticalLayout")
self.radioButton_a = QtWidgets.QRadioButton(self.frame)
self.radioButton_a.setEnabled(False)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.radioButton_a.sizePolicy().hasHeightForWidth())
self.radioButton_a.setSizePolicy(sizePolicy)
self.radioButton_a.setMinimumSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(12)
self.radioButton_a.setFont(font)
self.radioButton_a.setChecked(False)
self.radioButton_a.setObjectName("radioButton_a")
self.buttonGroupForIOChkOfIGCS_RX120GT = QtWidgets.QButtonGroup(Form)
self.buttonGroupForIOChkOfIGCS_RX120GT.setObjectName("buttonGroupForIOChkOfIGCS_RX120GT")
self.buttonGroupForIOChkOfIGCS_RX120GT.setExclusive(False)
self.buttonGroupForIOChkOfIGCS_RX120GT.addButton(self.radioButton_a)
self.verticalLayout.addWidget(self.radioButton_a)
self.labelToDisplayCopperLinkActAndStatusStatusForRX120 = QtWidgets.QLabel(self.frame)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.labelToDisplayCopperLinkActAndStatusStatusForRX120.sizePolicy().hasHeightForWidth())
self.labelToDisplayCopperLinkActAndStatusStatusForRX120.setSizePolicy(sizePolicy)
self.labelToDisplayCopperLinkActAndStatusStatusForRX120.setMinimumSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(14)
self.labelToDisplayCopperLinkActAndStatusStatusForRX120.setFont(font)
self.labelToDisplayCopperLinkActAndStatusStatusForRX120.setText("")
self.labelToDisplayCopperLinkActAndStatusStatusForRX120.setAlignment(QtCore.Qt.AlignCenter)
self.labelToDisplayCopperLinkActAndStatusStatusForRX120.setObjectName("labelToDisplayCopperLinkActAndStatusStatusForRX120")
self.verticalLayout.addWidget(self.labelToDisplayCopperLinkActAndStatusStatusForRX120)
self.radioButton_b = QtWidgets.QRadioButton(self.frame)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.radioButton_b.sizePolicy().hasHeightForWidth())
self.radioButton_b.setSizePolicy(sizePolicy)
self.radioButton_b.setMinimumSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(12)
self.radioButton_b.setFont(font)
self.radioButton_b.setChecked(False)
self.radioButton_b.setObjectName("radioButton_b")
self.buttonGroupForIOChkOfIGCS_RX120GT.addButton(self.radioButton_b)
self.verticalLayout.addWidget(self.radioButton_b)
self.labelToDisplayCopperLinkActAnd100MStatusForRX120 = QtWidgets.QLabel(self.frame)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.labelToDisplayCopperLinkActAnd100MStatusForRX120.sizePolicy().hasHeightForWidth())
self.labelToDisplayCopperLinkActAnd100MStatusForRX120.setSizePolicy(sizePolicy)
self.labelToDisplayCopperLinkActAnd100MStatusForRX120.setMinimumSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(14)
self.labelToDisplayCopperLinkActAnd100MStatusForRX120.setFont(font)
self.labelToDisplayCopperLinkActAnd100MStatusForRX120.setText("")
self.labelToDisplayCopperLinkActAnd100MStatusForRX120.setAlignment(QtCore.Qt.AlignCenter)
self.labelToDisplayCopperLinkActAnd100MStatusForRX120.setObjectName("labelToDisplayCopperLinkActAnd100MStatusForRX120")
self.verticalLayout.addWidget(self.labelToDisplayCopperLinkActAnd100MStatusForRX120)
self.radioButton_c = QtWidgets.QRadioButton(self.frame)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.radioButton_c.sizePolicy().hasHeightForWidth())
self.radioButton_c.setSizePolicy(sizePolicy)
self.radioButton_c.setMinimumSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(12)
self.radioButton_c.setFont(font)
self.radioButton_c.setChecked(False)
self.radioButton_c.setObjectName("radioButton_c")
self.buttonGroupForIOChkOfIGCS_RX120GT.addButton(self.radioButton_c)
self.verticalLayout.addWidget(self.radioButton_c)
self.labelToDisplaySFP1GStatusForRX120 = QtWidgets.QLabel(self.frame)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.labelToDisplaySFP1GStatusForRX120.sizePolicy().hasHeightForWidth())
self.labelToDisplaySFP1GStatusForRX120.setSizePolicy(sizePolicy)
self.labelToDisplaySFP1GStatusForRX120.setMinimumSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(14)
self.labelToDisplaySFP1GStatusForRX120.setFont(font)
self.labelToDisplaySFP1GStatusForRX120.setText("")
self.labelToDisplaySFP1GStatusForRX120.setAlignment(QtCore.Qt.AlignCenter)
self.labelToDisplaySFP1GStatusForRX120.setObjectName("labelToDisplaySFP1GStatusForRX120")
self.verticalLayout.addWidget(self.labelToDisplaySFP1GStatusForRX120)
self.gridLayout.addWidget(self.frame, 1, 0, 1, 2)
spacerItem = QtWidgets.QSpacerItem(74, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem, 2, 0, 1, 1)
self.pushButtonOK = QtWidgets.QPushButton(Form)
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(12)
self.pushButtonOK.setFont(font)
self.pushButtonOK.setObjectName("pushButtonOK")
self.gridLayout.addWidget(self.pushButtonOK, 2, 1, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
Form.setTabOrder(self.pushButtonOK, self.radioButton_a)
Form.setTabOrder(self.radioButton_a, self.radioButton_b)
Form.setTabOrder(self.radioButton_b, self.radioButton_c)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Tool"))
self.labelForItems.setText(_translate("Form", "Items:"))
self.radioButton_a.setText(_translate("Form", "A"))
self.radioButton_b.setText(_translate("Form", "B"))
self.radioButton_c.setText(_translate("Form", "C"))
self.pushButtonOK.setText(_translate("Form", "OK"))
However, the outcome did not turn out to be what I expected. The radio button at the top of the frame always remains in the disabled state though the other two radio buttons at the bottom could be disabled or enabled as I wish.
Can anyone kindly explain to me why? And how I should fix it?
I revise my code based on the answer of musicamante. And it works. Below is the revised version of my code:
m_view.py:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from main_window import Ui_Form
class MyMainWindow(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent = None):
super(MyMainWindow, self).__init__(parent)
self.setupUi(self)
self.destroyed.connect(sys.exit)
self.pushButtonOK.clicked.connect(self._update_state_of_radio_btns)
def _update_state_of_radio_btns(self):
radioBtnsAsList = self.frame.findChildren(QtWidgets.QRadioButton)
for radioBtn in radioBtnsAsList:
state = radioBtn.isEnabled()
radioBtn.setEnabled(not state)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyMainWindow()
window.show()
sys.exit(app.exec_())
main_window.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main_window_2.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# 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_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(176, 264)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/icons/ORing_icon.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Form.setWindowIcon(icon)
self.gridLayout = QtWidgets.QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.labelForItems = QtWidgets.QLabel(Form)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.labelForItems.sizePolicy().hasHeightForWidth())
self.labelForItems.setSizePolicy(sizePolicy)
self.labelForItems.setMinimumSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(12)
font.setBold(False)
font.setWeight(50)
self.labelForItems.setFont(font)
self.labelForItems.setStyleSheet("")
self.labelForItems.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.labelForItems.setObjectName("labelForItems")
self.gridLayout.addWidget(self.labelForItems, 0, 0, 1, 2)
self.frame = QtWidgets.QFrame(Form)
self.frame.setEnabled(True)
self.frame.setFrameShape(QtWidgets.QFrame.Panel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.verticalLayout = QtWidgets.QVBoxLayout(self.frame)
self.verticalLayout.setObjectName("verticalLayout")
self.radioButton_a = QtWidgets.QRadioButton(self.frame)
self.radioButton_a.setEnabled(True)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.radioButton_a.sizePolicy().hasHeightForWidth())
self.radioButton_a.setSizePolicy(sizePolicy)
self.radioButton_a.setMinimumSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(12)
self.radioButton_a.setFont(font)
self.radioButton_a.setChecked(False)
self.radioButton_a.setObjectName("radioButton_a")
self.buttonGroupForIOChkOfIGCS_RX120GT = QtWidgets.QButtonGroup(Form)
self.buttonGroupForIOChkOfIGCS_RX120GT.setObjectName("buttonGroupForIOChkOfIGCS_RX120GT")
self.buttonGroupForIOChkOfIGCS_RX120GT.setExclusive(False)
self.buttonGroupForIOChkOfIGCS_RX120GT.addButton(self.radioButton_a)
self.verticalLayout.addWidget(self.radioButton_a)
self.labelToDisplayCopperLinkActAndStatusStatusForRX120 = QtWidgets.QLabel(self.frame)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.labelToDisplayCopperLinkActAndStatusStatusForRX120.sizePolicy().hasHeightForWidth())
self.labelToDisplayCopperLinkActAndStatusStatusForRX120.setSizePolicy(sizePolicy)
self.labelToDisplayCopperLinkActAndStatusStatusForRX120.setMinimumSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(14)
self.labelToDisplayCopperLinkActAndStatusStatusForRX120.setFont(font)
self.labelToDisplayCopperLinkActAndStatusStatusForRX120.setText("")
self.labelToDisplayCopperLinkActAndStatusStatusForRX120.setAlignment(QtCore.Qt.AlignCenter)
self.labelToDisplayCopperLinkActAndStatusStatusForRX120.setObjectName("labelToDisplayCopperLinkActAndStatusStatusForRX120")
self.verticalLayout.addWidget(self.labelToDisplayCopperLinkActAndStatusStatusForRX120)
self.radioButton_b = QtWidgets.QRadioButton(self.frame)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.radioButton_b.sizePolicy().hasHeightForWidth())
self.radioButton_b.setSizePolicy(sizePolicy)
self.radioButton_b.setMinimumSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(12)
self.radioButton_b.setFont(font)
self.radioButton_b.setChecked(False)
self.radioButton_b.setObjectName("radioButton_b")
self.buttonGroupForIOChkOfIGCS_RX120GT.addButton(self.radioButton_b)
self.verticalLayout.addWidget(self.radioButton_b)
self.labelToDisplayCopperLinkActAnd100MStatusForRX120 = QtWidgets.QLabel(self.frame)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.labelToDisplayCopperLinkActAnd100MStatusForRX120.sizePolicy().hasHeightForWidth())
self.labelToDisplayCopperLinkActAnd100MStatusForRX120.setSizePolicy(sizePolicy)
self.labelToDisplayCopperLinkActAnd100MStatusForRX120.setMinimumSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(14)
self.labelToDisplayCopperLinkActAnd100MStatusForRX120.setFont(font)
self.labelToDisplayCopperLinkActAnd100MStatusForRX120.setText("")
self.labelToDisplayCopperLinkActAnd100MStatusForRX120.setAlignment(QtCore.Qt.AlignCenter)
self.labelToDisplayCopperLinkActAnd100MStatusForRX120.setObjectName("labelToDisplayCopperLinkActAnd100MStatusForRX120")
self.verticalLayout.addWidget(self.labelToDisplayCopperLinkActAnd100MStatusForRX120)
self.radioButton_c = QtWidgets.QRadioButton(self.frame)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.radioButton_c.sizePolicy().hasHeightForWidth())
self.radioButton_c.setSizePolicy(sizePolicy)
self.radioButton_c.setMinimumSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(12)
self.radioButton_c.setFont(font)
self.radioButton_c.setChecked(False)
self.radioButton_c.setObjectName("radioButton_c")
self.buttonGroupForIOChkOfIGCS_RX120GT.addButton(self.radioButton_c)
self.verticalLayout.addWidget(self.radioButton_c)
self.labelToDisplaySFP1GStatusForRX120 = QtWidgets.QLabel(self.frame)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.labelToDisplaySFP1GStatusForRX120.sizePolicy().hasHeightForWidth())
self.labelToDisplaySFP1GStatusForRX120.setSizePolicy(sizePolicy)
self.labelToDisplaySFP1GStatusForRX120.setMinimumSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(14)
self.labelToDisplaySFP1GStatusForRX120.setFont(font)
self.labelToDisplaySFP1GStatusForRX120.setText("")
self.labelToDisplaySFP1GStatusForRX120.setAlignment(QtCore.Qt.AlignCenter)
self.labelToDisplaySFP1GStatusForRX120.setObjectName("labelToDisplaySFP1GStatusForRX120")
self.verticalLayout.addWidget(self.labelToDisplaySFP1GStatusForRX120)
self.gridLayout.addWidget(self.frame, 1, 0, 1, 2)
spacerItem = QtWidgets.QSpacerItem(74, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem, 2, 0, 1, 1)
self.pushButtonOK = QtWidgets.QPushButton(Form)
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(12)
self.pushButtonOK.setFont(font)
self.pushButtonOK.setObjectName("pushButtonOK")
self.gridLayout.addWidget(self.pushButtonOK, 2, 1, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
Form.setTabOrder(self.pushButtonOK, self.radioButton_a)
Form.setTabOrder(self.radioButton_a, self.radioButton_b)
Form.setTabOrder(self.radioButton_b, self.radioButton_c)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Tool"))
self.labelForItems.setText(_translate("Form", "Items:"))
self.radioButton_a.setText(_translate("Form", "A"))
self.radioButton_b.setText(_translate("Form", "B"))
self.radioButton_c.setText(_translate("Form", "C"))
self.pushButtonOK.setText(_translate("Form", "OK"))
I am trying to build an AI voice recognition app with a GUI to show the speech recognition text as well as my pre-determined outcomes as a text inside different labels in pyqt6 program. the problem is how do I set the label's text to change dynamically based on the the recognized speech (output as text) and next step of the AI's predetermined outcome(also output as text) on the pyqt app labels.
A normal import of the AI.py file into the QT6.py file doesn't dynamically change the text.
Combining both into the same file runs either the program (which has nested loops to carry outcomes based on user's speech query) or the pyqt window whichever is run earlier. the combined file doesn't run one after the other.
What I am looking to acheive:
labels inside pyqt app window to change and show text dynamically based on how the text output changes in the AI app.
Here is my qt6.py child file:
from main_ui import UI_Extension
from PyQt6 import QtWidgets
import sys
class U_interface(UI_Extension):
def setupUi(self, Form):
super().setupUi(Form)
self.alerts.setText("Liability Warning")
self.alert_description.setText("Liability description")
self.ai_response.setText(Dynamic_text) # from AI.py file
self.mic_recording_state.setText("Listening...(test)")
self.speech_recognition.setText(Dynamic_speech_recognition_text) # from AI.py file
self.management_plan.setText("EKG scheduled for the 24th of November 2022")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = U_interface()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec())
Here is my qt6.py parent file converted from qt_designer .ui file
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1280, 800)
Form.setAutoFillBackground(False)
Form.setStyleSheet("")
self.combined_layout = QtWidgets.QWidget(Form)
self.combined_layout.setGeometry(QtCore.QRect(0, 0, 1280, 800))
self.combined_layout.setStyleSheet("QPushButton#ReviewCase{\n"
" background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(33, 124, 173, 255), stop:1 rgba(31, 118, 165, 142));\n"
" color:rgba(255,255,255,210);\n"
" border-radius: 5px;\n"
"}\n"
"\n"
"QPushButton#ReviewCase:hover{\n"
" background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(14, 208, 75, 255), stop:1 rgba(26, 255, 0, 213));\n"
" color:rgba(255,255,255,210);\n"
" color:rgba(0,0,0,210);\n"
"}\n"
"\n"
"QPushButton#ReviewCase:pressed{\n"
" padding-left:5px;\n"
" padding-right:5px;\n"
" color:rgba(255,255,255,210);\n"
" background-color: rgba(105,118,132,200);\n"
"}\n"
"\n"
"QPushButton#ExitProgram{\n"
" background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(33, 124, 173, 255), stop:1 rgba(31, 118, 165, 142));\n"
" color:rgba(255,255,255,210);\n"
" border-radius: 5px;\n"
"}\n"
"\n"
"QPushButton#ExitProgram:hover{\n"
" background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(255, 0, 4, 255), stop:1 rgba(255, 0, 0, 111));\n"
" color:rgba(255,255,255,210);\n"
"}\n"
"\n"
"QPushButton#ExitProgram:pressed{\n"
" padding-left:5px;\n"
" padding-right:5px;\n"
" background-color: rgba(105,118,132,200);\n"
"}\n"
"")
self.combined_layout.setObjectName("combined_layout")
self.background_image = QtWidgets.QLabel(self.combined_layout)
self.background_image.setGeometry(QtCore.QRect(0, 0, 1280, 800))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Ignored, QtWidgets.QSizePolicy.Policy.Ignored)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.background_image.sizePolicy().hasHeightForWidth())
self.background_image.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(10)
self.background_image.setFont(font)
self.background_image.setContextMenuPolicy(QtCore.Qt.ContextMenuPolicy.DefaultContextMenu)
self.background_image.setStyleSheet("border-image: url(:/images/background);")
self.background_image.setText("")
self.background_image.setScaledContents(True)
self.background_image.setObjectName("background_image")
self.doc_logo = QtWidgets.QLabel(self.combined_layout)
self.doc_logo.setGeometry(QtCore.QRect(15, 16, 41, 41))
self.doc_logo.setStyleSheet("border-image: url(:/images/doctor);")
self.doc_logo.setText("")
self.doc_logo.setObjectName("doc_logo")
self.horizontalLayoutWidget = QtWidgets.QWidget(self.combined_layout)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 743, 1261, 51))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setSizeConstraint(QtWidgets.QLayout.SizeConstraint.SetDefaultConstraint)
self.horizontalLayout.setContentsMargins(5, 0, 5, 0)
self.horizontalLayout.setSpacing(10)
self.horizontalLayout.setObjectName("horizontalLayout")
self.ExitProgram = QtWidgets.QPushButton(self.horizontalLayoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Maximum, QtWidgets.QSizePolicy.Policy.Maximum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.ExitProgram.sizePolicy().hasHeightForWidth())
self.ExitProgram.setSizePolicy(sizePolicy)
self.ExitProgram.setMinimumSize(QtCore.QSize(200, 35))
self.ExitProgram.setBaseSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
font.setKerning(True)
self.ExitProgram.setFont(font)
self.ExitProgram.setCursor(QtGui.QCursor(QtCore.Qt.CursorShape.PointingHandCursor))
self.ExitProgram.setStyleSheet("")
self.ExitProgram.setObjectName("ExitProgram")
self.horizontalLayout.addWidget(self.ExitProgram)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.mic_recording_state = QtWidgets.QLabel(self.horizontalLayoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.mic_recording_state.sizePolicy().hasHeightForWidth())
self.mic_recording_state.setSizePolicy(sizePolicy)
self.mic_recording_state.setMinimumSize(QtCore.QSize(770, 25))
font = QtGui.QFont()
font.setPointSize(20)
font.setBold(False)
self.mic_recording_state.setFont(font)
self.mic_recording_state.setStyleSheet("color: #c6d4e2;")
self.mic_recording_state.setText("")
self.mic_recording_state.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.mic_recording_state.setIndent(0)
self.mic_recording_state.setObjectName("mic_recording_state")
self.horizontalLayout.addWidget(self.mic_recording_state)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.ReviewCase = QtWidgets.QPushButton(self.horizontalLayoutWidget)
self.ReviewCase.setEnabled(True)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Maximum, QtWidgets.QSizePolicy.Policy.Maximum)
sizePolicy.setHorizontalStretch(60)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.ReviewCase.sizePolicy().hasHeightForWidth())
self.ReviewCase.setSizePolicy(sizePolicy)
self.ReviewCase.setMinimumSize(QtCore.QSize(200, 35))
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
self.ReviewCase.setFont(font)
self.ReviewCase.setCursor(QtGui.QCursor(QtCore.Qt.CursorShape.PointingHandCursor))
self.ReviewCase.setAutoFillBackground(False)
self.ReviewCase.setStyleSheet("")
self.ReviewCase.setObjectName("ReviewCase")
self.horizontalLayout.addWidget(self.ReviewCase)
self.layoutWidget = QtWidgets.QWidget(self.combined_layout)
self.layoutWidget.setGeometry(QtCore.QRect(100, 40, 661, 681))
self.layoutWidget.setObjectName("layoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setSpacing(20)
self.verticalLayout.setObjectName("verticalLayout")
self.alerts = QtWidgets.QLabel(self.layoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Preferred, QtWidgets.QSizePolicy.Policy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.alerts.sizePolicy().hasHeightForWidth())
self.alerts.setSizePolicy(sizePolicy)
self.alerts.setMinimumSize(QtCore.QSize(0, 50))
font = QtGui.QFont()
font.setFamily("Cambria")
font.setPointSize(22)
font.setBold(True)
self.alerts.setFont(font)
self.alerts.setToolTip("")
self.alerts.setStyleSheet("color: rgb(255, 116, 69);")
self.alerts.setText("")
self.alerts.setAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter|QtCore.Qt.AlignmentFlag.AlignTop)
self.alerts.setIndent(0)
self.alerts.setObjectName("alerts")
self.verticalLayout.addWidget(self.alerts)
self.alert_description = QtWidgets.QLabel(self.layoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Preferred, QtWidgets.QSizePolicy.Policy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.alert_description.sizePolicy().hasHeightForWidth())
self.alert_description.setSizePolicy(sizePolicy)
self.alert_description.setMinimumSize(QtCore.QSize(0, 50))
font = QtGui.QFont()
font.setPointSize(22)
font.setBold(True)
self.alert_description.setFont(font)
self.alert_description.setStyleSheet("color: rgb(43, 255, 121);")
self.alert_description.setText("")
self.alert_description.setAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter|QtCore.Qt.AlignmentFlag.AlignTop)
self.alert_description.setIndent(0)
self.alert_description.setObjectName("alert_description")
self.verticalLayout.addWidget(self.alert_description)
self.ai_response = QtWidgets.QLabel(self.layoutWidget)
font = QtGui.QFont()
font.setFamily("Cambria")
font.setPointSize(22)
font.setBold(False)
self.ai_response.setFont(font)
self.ai_response.setStyleSheet("padding: 23 33 23;\n"
"color: #c6d4e2;\n"
"\n"
"")
self.ai_response.setFrameShape(QtWidgets.QFrame.Shape.NoFrame)
self.ai_response.setLineWidth(1)
self.ai_response.setText("")
self.ai_response.setScaledContents(False)
self.ai_response.setAlignment(QtCore.Qt.AlignmentFlag.AlignJustify|QtCore.Qt.AlignmentFlag.AlignVCenter)
self.ai_response.setWordWrap(True)
self.ai_response.setIndent(0)
self.ai_response.setObjectName("ai_response")
self.verticalLayout.addWidget(self.ai_response)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setSpacing(0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.gif = QtWidgets.QLabel(self.layoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Ignored, QtWidgets.QSizePolicy.Policy.Ignored)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.gif.sizePolicy().hasHeightForWidth())
self.gif.setSizePolicy(sizePolicy)
self.gif.setMinimumSize(QtCore.QSize(240, 180))
self.gif.setStyleSheet("")
self.gif.setText("")
self.gif.setScaledContents(True)
self.gif.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.gif.setIndent(0)
self.gif.setObjectName("gif")
self.horizontalLayout_2.addWidget(self.gif)
self.speech_recognition = QtWidgets.QLabel(self.layoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Preferred, QtWidgets.QSizePolicy.Policy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(170)
sizePolicy.setHeightForWidth(self.speech_recognition.sizePolicy().hasHeightForWidth())
self.speech_recognition.setSizePolicy(sizePolicy)
self.speech_recognition.setMinimumSize(QtCore.QSize(0, 170))
font = QtGui.QFont()
font.setPointSize(16)
self.speech_recognition.setFont(font)
self.speech_recognition.setStyleSheet("padding: 29 33 29;\n"
"color: #c6d4e2;")
self.speech_recognition.setText("")
self.speech_recognition.setAlignment(QtCore.Qt.AlignmentFlag.AlignJustify|QtCore.Qt.AlignmentFlag.AlignVCenter)
self.speech_recognition.setWordWrap(True)
self.speech_recognition.setIndent(0)
self.speech_recognition.setTextInteractionFlags(QtCore.Qt.TextInteractionFlag.NoTextInteraction)
self.speech_recognition.setObjectName("speech_recognition")
self.horizontalLayout_2.addWidget(self.speech_recognition)
self.horizontalLayout_2.setStretch(0, 3)
self.horizontalLayout_2.setStretch(1, 6)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.verticalLayout.setStretch(0, 1)
self.verticalLayout.setStretch(2, 6)
self.verticalLayout.setStretch(3, 4)
self.layoutWidget1 = QtWidgets.QWidget(self.combined_layout)
self.layoutWidget1.setGeometry(QtCore.QRect(850, 40, 411, 681))
self.layoutWidget1.setObjectName("layoutWidget1")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.layoutWidget1)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setSpacing(15)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.management = QtWidgets.QLabel(self.layoutWidget1)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Preferred, QtWidgets.QSizePolicy.Policy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.management.sizePolicy().hasHeightForWidth())
self.management.setSizePolicy(sizePolicy)
self.management.setMinimumSize(QtCore.QSize(0, 50))
self.management.setStyleSheet("font: 700 22pt \"Segoe UI Variable Text\";\n"
"color: rgb(153, 255, 199);")
self.management.setAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter|QtCore.Qt.AlignmentFlag.AlignTop)
self.management.setIndent(0)
self.management.setObjectName("management")
self.verticalLayout_2.addWidget(self.management)
self.management_plan = QtWidgets.QTextBrowser(self.layoutWidget1)
font = QtGui.QFont()
font.setPointSize(18)
font.setBold(True)
self.management_plan.setFont(font)
self.management_plan.viewport().setProperty("cursor", QtGui.QCursor(QtCore.Qt.CursorShape.ArrowCursor))
self.management_plan.setStyleSheet("background-color: rgb(7, 18, 40);\n"
"border-radius: 10px;\n"
"padding: 25 25 25 25;\n"
"color: #c6d4e2;")
self.management_plan.setInputMethodHints(QtCore.Qt.InputMethodHint.ImhMultiLine)
self.management_plan.setTextInteractionFlags(QtCore.Qt.TextInteractionFlag.LinksAccessibleByKeyboard|QtCore.Qt.TextInteractionFlag.LinksAccessibleByMouse|QtCore.Qt.TextInteractionFlag.TextBrowserInteraction|QtCore.Qt.TextInteractionFlag.TextSelectableByKeyboard|QtCore.Qt.TextInteractionFlag.TextSelectableByMouse)
self.management_plan.setOpenExternalLinks(True)
self.management_plan.setObjectName("management_plan")
self.verticalLayout_2.addWidget(self.management_plan)
self.verticalLayout_2.setStretch(0, 1)
self.verticalLayout_2.setStretch(1, 12)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.ExitProgram.setText(_translate("Form", "Exit Program"))
self.ReviewCase.setText(_translate("Form", "Review Case"))
self.management.setText(_translate("Form", "Management"))
self.management_plan.setHtml(_translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><meta charset=\"utf-8\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"hr { height: 1px; border-width: 0; }\n"
"</style></head><body style=\" font-family:\'Segoe UI Variable Text\'; font-size:18pt; font-weight:700; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:14pt;\"><br /></p></body></html>"))
Here is my AI.py file:
from playsound import playsound
import sounddevice as sd
from gtts import gTTS
import argparse
import random
import queue
import vosk
import json
import sys
import os
q = queue.Queue()
model = vosk.Model("c:......Model (EN-US)")
def greeting():
..........
def microphone():
..........
speech_recognition_text = "xyz........" # to be printed inside the label in qt6.py file
def recorded_audio():
............
def speak(transcription):
............
try:
............
stream = sd.RawInputStream(samplerate=args.samplerate, blocksize = 8000, device=args.device, dtype='int16', channels=1, callback=callback)
with stream:
greeting()
while True:
audio = recorded_audio()
if ('hello' in audio):
Dynamic_text = "abc...." # to be printed inside the label in qt6.py file
elif ('all right' in audio):
DynamicText = "def......" # to be printed inside the label in qt6.py file
except KeyboardInterrupt:
print('\nShutting Down')
parser.exit(0)
except Exception as e:
parser.exit(type(e).__name__ + ': ' + str(e))
I am under the impression that QThread is to be used somehow. Dont know how and where.
Any help is greatly appreciated.
I don't understand how to put data into QTableWidget row by row. I take data from lineEdit then put it into table, but when I try to add another person with data it rewrites the previous data, but I need to put it in the row below. I guess I should do something with "row"
Here is my code
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import *
import sqlite3
import sys
conn = sqlite3.connect('todo.db')
cur = conn.cursor()
cur.execute('CREATE TABLE if not exists disp_list(disp_name text, disp_surname text, disp_date text)')
conn.commit()
conn.close()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(960, 550)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(610, 261, 341, 241))
self.listWidget.setObjectName("listWidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(610, 180, 331, 71))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
self.label.setFont(font)
self.label.setWordWrap(True)
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(10, 40, 311, 41))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(340, 40, 311, 41))
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(690, 40, 251, 41))
self.lineEdit_3.setObjectName("lineEdit_3")
self.pushButton = QtWidgets.QPushButton(self.centralwidget, clicked=lambda: self.add_disp())
self.pushButton.setGeometry(QtCore.QRect(30, 100, 221, 71))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(14)
font.setBold(False)
font.setWeight(50)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(270, 100, 221, 71))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(14)
font.setBold(False)
font.setWeight(50)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(720, 100, 221, 71))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(14)
font.setBold(False)
font.setWeight(50)
self.pushButton_4.setFont(font)
self.pushButton_4.setObjectName("pushButton_4")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(10, 200, 591, 301))
self.tableWidget.setRowCount(130)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(3)
item = QtWidgets.QTableWidgetItem()
font = QtGui.QFont()
font.setPointSize(18)
item.setFont(font)
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
self.tableWidget.horizontalHeader().setDefaultSectionSize(180)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(20, 10, 291, 16))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(350, 10, 291, 16))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(690, 10, 251, 20))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(510, 100, 191, 71))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(14)
font.setBold(False)
font.setWeight(50)
self.pushButton_5.setFont(font)
self.pushButton_5.setObjectName("pushButton_5")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 960, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def add_disp(self):
name = self.lineEdit.text()
surname = self.lineEdit_2.text()
date = self.lineEdit_3.text()
disps=[{"disp_name": name, "disp_surname": surname, "disp_date": date}]
for person in disps:
row = 1
self.tableWidget.setItem(row, 0, QtWidgets.QTableWidgetItem(person["disp_name"]))
self.tableWidget.setItem(row, 1, QtWidgets.QTableWidgetItem(person["disp_surname"]))
self.tableWidget.setItem(row, 2, QtWidgets.QTableWidgetItem(person["disp_date"]))
name = self.lineEdit.setText("")
surname = self.lineEdit_2.setText("")
date = self.lineEdit_3.setText("")
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Список диспетчеров, у которых заканчивается срок"))
self.pushButton.setText(_translate("MainWindow", "Add ATC"))
self.pushButton_3.setText(_translate("MainWindow", "Delete from table"))
self.pushButton_4.setText(_translate("MainWindow", "Delete from database"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Name"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "Surname"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "Issue_date"))
self.label_2.setText(_translate("MainWindow", "Enter name"))
self.label_3.setText(_translate("MainWindow", "Enter surname"))
self.label_4.setText(_translate("MainWindow", "Enter date"))
self.pushButton_5.setText(_translate("MainWindow", "Save to database"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Supposing that you want to insert items incrementally, you should create an instance attribute that will be used to start with the first row (which is 0, not 1, by the way) and then increment it by 1 every time.
This, anyway, should not be done with a file created by pyuic, because those files are meant to be left unmodified. Editing them is highly discouraged and considered bad practice for a lot of reasons. Read more about how to properly use those files when using Designer.
So, rebuild again your .ui file (in the next example I'm supposing you've generated a file named ui_mainwindow.py), and then create your actual script similar to the following:
from PyQt5 import QtCore, QtGui, QtWidgets
from ui_mainwindow import Ui_MainWindow
import sqlite3
import sys
conn = sqlite3.connect('todo.db')
cur = conn.cursor()
cur.execute('CREATE TABLE if not exists disp_list(disp_name text, disp_surname text, disp_date text)')
conn.commit()
conn.close()
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.currentRow = -1
self.pushButton.clicked.connect(self.add_disp)
def add_disp(self):
name = self.lineEdit.text()
surname = self.lineEdit_2.text()
date = self.lineEdit_3.text()
self.currentRow += 1
if self.currentRow > self.tableWidget.rowCount() - 1:
self.tableWidget.insertRow(self.tableWidget.rowCount())
self.tableWidget.setItem(self.currentRow, 0,
QtWidgets.QTableWidgetItem(name))
self.tableWidget.setItem(self.currentRow, 1,
QtWidgets.QTableWidgetItem(surname))
self.tableWidget.setItem(self.currentRow, 2,
QtWidgets.QTableWidgetItem(date))
self.lineEdit.clear()
self.lineEdit_2.clear()
self.lineEdit_3.clear()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
I have created a QMainWindow and a customized QGroupBox by Qt Designer and compiled to .py file.
This is my widgets hierarchy: QMainWindow -> QStackedWidget -> QScrollArea -> QVBoxLayout -> QGroupBox(es).
But when I tried to add the customized QGroupBox (class Ui_deck) I have created above to the QVBoxLayout (belongs to class Ui_main), it shows nothing.
Here is my code:
#main.py
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
from main_ui import Ui_main
from deck_name import Ui_deck
class Main(QtWidgets.QMainWindow, Ui_main):
def __init__(self):
super().__init__()
self.setupUi(self)
groupbox = QtWidgets.QGroupBox(self.scroll_widget)
deck = Ui_deck()
deck.setupUi(groupbox)
self.scroll_layout.addWidget(groupbox)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main = QtWidgets.QMainWindow()
ui = Main()
ui.setupUi(main)
main.show()
sys.exit(app.exec_())
#main_ui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'deck_name.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# 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_main(object):
def setupUi(self, main):
main.setObjectName("main")
main.resize(621, 522)
main.setMinimumSize(QtCore.QSize(0, 100))
main.setStyleSheet("")
self.centralwidget = QtWidgets.QWidget(main)
self.centralwidget.setStyleSheet("")
self.centralwidget.setObjectName("centralwidget")
self.stacked = QtWidgets.QStackedWidget(self.centralwidget)
self.stacked.setGeometry(QtCore.QRect(0, 0, 621, 501))
self.stacked.setObjectName("stacked")
self.home = QtWidgets.QWidget()
self.home.setObjectName("home")
self.scroll = QtWidgets.QScrollArea(self.home)
self.scroll.setGeometry(QtCore.QRect(0, 0, 621, 511))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.scroll.sizePolicy().hasHeightForWidth())
self.scroll.setSizePolicy(sizePolicy)
self.scroll.setStyleSheet("QScrollBar:vertical{\n"
" width: 5px;\n"
" border: none;\n"
"}\n"
"\n"
"QScrollBar::handle:vertical{\n"
" border:0;\n"
" background-color: rgb(176, 176, 176);\n"
" border-radius: 2px;\n"
"}\n"
"\n"
"QScrollBar:up-arrow:vertical, QScrollBar:down-arrow:vertical{\n"
" border: none;\n"
" background-color: none;\n"
" color: none;\n"
"}\n"
"\n"
"QScrollBar::add-line:vertical {\n"
" border: none;\n"
" background: none;\n"
"}\n"
"\n"
"QScrollBar::sub-line:vertical {\n"
" border: none;\n"
" background: none;\n"
"}")
self.scroll.setFrameShadow(QtWidgets.QFrame.Plain)
self.scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.scroll.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
self.scroll.setWidgetResizable(True)
self.scroll.setObjectName("scroll")
self.scroll_widget = QtWidgets.QWidget()
self.scroll_widget.setGeometry(QtCore.QRect(0, 0, 619, 509))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.scroll_widget.sizePolicy().hasHeightForWidth())
self.scroll_widget.setSizePolicy(sizePolicy)
self.scroll_widget.setStyleSheet("QPushButton{\n"
" height: 100px;\n"
"}\n"
"QWidget#scroll_widget{\n"
" background-color: white;\n"
"}\n"
"")
self.scroll_widget.setObjectName("scroll_widget")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.scroll_widget)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.scroll_layout = QtWidgets.QVBoxLayout()
self.scroll_layout.setSizeConstraint(QtWidgets.QLayout.SetFixedSize)
self.scroll_layout.setObjectName("scroll_layout")
self.buttons = QtWidgets.QGroupBox(self.scroll_widget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.buttons.sizePolicy().hasHeightForWidth())
self.buttons.setSizePolicy(sizePolicy)
self.buttons.setMinimumSize(QtCore.QSize(0, 100))
font = QtGui.QFont()
font.setFamily("Arial")
self.buttons.setFont(font)
self.buttons.setStyleSheet("QGroupBox#buttons{\n"
" border: 0;\n"
"}")
self.buttons.setTitle("")
self.buttons.setObjectName("buttons")
self.label = QtWidgets.QLabel(self.buttons)
self.label.setGeometry(QtCore.QRect(8, 0, 591, 50))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
self.label.setSizePolicy(sizePolicy)
self.label.setMinimumSize(QtCore.QSize(0, 50))
font = QtGui.QFont()
font.setPointSize(16)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.label.setObjectName("label")
self.add_deck = QtWidgets.QPushButton(self.buttons)
self.add_deck.setGeometry(QtCore.QRect(10, 50, 129, 40))
self.add_deck.setMaximumSize(QtCore.QSize(16777215, 50))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(10)
self.add_deck.setFont(font)
self.add_deck.setStyleSheet("QPushButton#add_deck{\n"
" border: 0;\n"
" border-radius: 20px;\n"
" background-color: rgb(132, 255, 16)\n"
"}\n"
"\n"
"QPushButton#add_deck:hover{\n"
" background-color: rgb(125, 234, 0)\n"
"}\n"
"\n"
"")
self.add_deck.setObjectName("add_deck")
self.scroll_layout.addWidget(self.buttons)
self.verticalLayout_3.addLayout(self.scroll_layout)
self.scroll.setWidget(self.scroll_widget)
self.stacked.addWidget(self.home)
self.page_2 = QtWidgets.QWidget()
self.page_2.setObjectName("page_2")
self.stacked.addWidget(self.page_2)
main.setCentralWidget(self.centralwidget)
self.retranslateUi(main)
self.stacked.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(main)
def retranslateUi(self, main):
_translate = QtCore.QCoreApplication.translate
main.setWindowTitle(_translate("main", "Flashcards"))
self.label.setText(_translate("main", "All decks"))
self.add_deck.setText(_translate("main", "Add deck"))
#deck_name.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'deck_name.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# 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_deck(object):
def setupUi(self, deck):
deck.setObjectName("deck")
deck.resize(565, 200)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(deck.sizePolicy().hasHeightForWidth())
deck.setSizePolicy(sizePolicy)
deck.setMinimumSize(QtCore.QSize(0, 200))
deck.setWindowTitle("")
self.gridLayoutWidget = QtWidgets.QWidget(deck)
self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 0, 561, 201))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.deck_name = QtWidgets.QLabel(self.gridLayoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.deck_name.sizePolicy().hasHeightForWidth())
self.deck_name.setSizePolicy(sizePolicy)
self.deck_name.setMinimumSize(QtCore.QSize(0, 50))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(13)
self.deck_name.setFont(font)
self.deck_name.setObjectName("deck_name")
self.gridLayout.addWidget(self.deck_name, 0, 0, 1, 1)
self.button = QtWidgets.QFrame(self.gridLayoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.button.sizePolicy().hasHeightForWidth())
self.button.setSizePolicy(sizePolicy)
self.button.setMinimumSize(QtCore.QSize(0, 30))
self.button.setMaximumSize(QtCore.QSize(16777215, 50))
font = QtGui.QFont()
font.setFamily("Arial")
self.button.setFont(font)
self.button.setStyleSheet("QPushButton{\n"
" border-radius: 12px;\n"
"}")
self.button.setObjectName("button")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.button)
self.horizontalLayout.setObjectName("horizontalLayout")
self.review = QtWidgets.QPushButton(self.button)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.review.sizePolicy().hasHeightForWidth())
self.review.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
self.review.setFont(font)
self.review.setStyleSheet("QPushButton#review{\n"
" background-color: rgb(132, 255, 16);\n"
"}\n"
"\n"
"QPushButton#review:hover{\n"
" background-color: rgb(125, 234, 0)\n"
"}\n"
"")
self.review.setObjectName("review")
self.horizontalLayout.addWidget(self.review)
self.flash = QtWidgets.QPushButton(self.button)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.flash.sizePolicy().hasHeightForWidth())
self.flash.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
self.flash.setFont(font)
self.flash.setStyleSheet("QPushButton#flash{\n"
" background-color:rgb(132, 255, 16);\n"
"}\n"
"\n"
"QPushButton#flash:hover{\n"
" background-color: rgb(125, 234, 0)\n"
"}\n"
"")
self.flash.setObjectName("flash")
self.horizontalLayout.addWidget(self.flash)
self.word_list = QtWidgets.QPushButton(self.button)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.word_list.sizePolicy().hasHeightForWidth())
self.word_list.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
self.word_list.setFont(font)
self.word_list.setStyleSheet("QPushButton#word_list{\n"
" background-color:rgb(132, 255, 16);\n"
"}\n"
"\n"
"QPushButton#word_list:hover{\n"
" background-color: rgb(125, 234, 0)\n"
"}\n"
"")
self.word_list.setObjectName("word_list")
self.horizontalLayout.addWidget(self.word_list)
self.gridLayout.addWidget(self.button, 1, 0, 1, 1)
self.dropdown = QtWidgets.QToolButton(self.gridLayoutWidget)
font = QtGui.QFont()
font.setPointSize(10)
self.dropdown.setFont(font)
self.dropdown.setStyleSheet("border: none")
self.dropdown.setObjectName("dropdown")
self.gridLayout.addWidget(self.dropdown, 0, 1, 1, 1)
self.retranslateUi(deck)
QtCore.QMetaObject.connectSlotsByName(deck)
def retranslateUi(self, deck):
_translate = QtCore.QCoreApplication.translate
self.deck_name.setText(_translate("deck", "Deck_name"))
self.review.setText(_translate("deck", "Review"))
self.flash.setText(_translate("deck", "Flashcards mode"))
self.word_list.setText(_translate("deck", "Word list mode"))
self.dropdown.setText(_translate("deck", "..."))
Here is the output:
https://drive.google.com/file/d/1uAtbH72oS9NdBzeEOMhW77ZXYZTBMycg/view?usp=sharing
It is supposed to have two groupboxes in the window
You are showing an instance of QtWidgets.QMainWindow instead of Main. Try changing the last part of main.py to
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())
I'm using PyQt5 designer to make a little application. I mostly work with designer rather than building from scratch so this might be a silly question. Basically i have a main menu and a "create" button that when clicked will open up the other pyqt5 .py file (also created in designer) i have been working on.
So far i complied the ui files into .py files and imported the popup window that i want to be able to generate more than one of and then i use this code to some success:
I have not added any additonal code to the converted ui files excpect this function:
Here is some code from my mainmenu and popoutwindow .py files:
this is the setupUi being called from my file im trying to replicate
class BookPopout(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(772, 685)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lastNameLabel = QtWidgets.QLabel(self.centralwidget)
self.lastNameLabel.setGeometry(QtCore.QRect(20, 110, 60, 17))
self.lastNameLabel.setObjectName("lastNameLabel")
self.cityEntry = QtWidgets.QLineEdit(self.centralwidget)
self.cityEntry.setGeometry(QtCore.QRect(20, 310, 391, 27))
self.cityEntry.setReadOnly(True)
self.cityEntry.setObjectName("cityEntry")
self.bookTable = QtWidgets.QTableWidget(self.centralwidget)
self.bookTable.setGeometry(QtCore.QRect(470, 10, 281, 481))
font = QtGui.QFont()
font.setPointSize(7)
self.bookTable.setFont(font)
self.bookTable.setAutoFillBackground(False)
self.bookTable.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self.bookTable.setRowCount(1)
self.bookTable.setColumnCount(1)
self.bookTable.setObjectName("bookTable")
item = QtWidgets.QTableWidgetItem()
self.bookTable.setItem(0, 0, item)
self.bookTable.horizontalHeader().setVisible(False)
self.bookTable.horizontalHeader().setCascadingSectionResizes(False)
self.bookTable.horizontalHeader().setDefaultSectionSize(100)
self.bookTable.horizontalHeader().setStretchLastSection(True)
self.addressLineOneEntry = QtWidgets.QLineEdit(self.centralwidget)
self.addressLineOneEntry.setGeometry(QtCore.QRect(20, 190, 391, 27))
self.addressLineOneEntry.setReadOnly(True)
self.addressLineOneEntry.setObjectName("addressLineOneEntry")
self.stateEntry = QtWidgets.QLineEdit(self.centralwidget)
self.stateEntry.setGeometry(QtCore.QRect(20, 380, 391, 27))
self.stateEntry.setReadOnly(True)
self.stateEntry.setObjectName("stateEntry")
self.firstNameEntry = QtWidgets.QLineEdit(self.centralwidget)
self.firstNameEntry.setGeometry(QtCore.QRect(20, 70, 391, 27))
self.firstNameEntry.setReadOnly(True)
self.firstNameEntry.setObjectName("firstNameEntry")
self.streetAddressLabel = QtWidgets.QLabel(self.centralwidget)
self.streetAddressLabel.setGeometry(QtCore.QRect(20, 170, 83, 17))
self.streetAddressLabel.setObjectName("streetAddressLabel")
self.streetAddressTwoLabel = QtWidgets.QLabel(self.centralwidget)
self.streetAddressTwoLabel.setGeometry(QtCore.QRect(20, 240, 220, 17))
self.streetAddressTwoLabel.setObjectName("streetAddressTwoLabel")
self.phoneNumberLabel = QtWidgets.QLabel(self.centralwidget)
self.phoneNumberLabel.setGeometry(QtCore.QRect(20, 480, 87, 17))
self.phoneNumberLabel.setObjectName("phoneNumberLabel")
self.addressLineTwoEntry = QtWidgets.QLineEdit(self.centralwidget)
self.addressLineTwoEntry.setGeometry(QtCore.QRect(20, 260, 391, 27))
self.addressLineTwoEntry.setReadOnly(True)
self.addressLineTwoEntry.setObjectName("addressLineTwoEntry")
self.phoneNumberEntry = QtWidgets.QLineEdit(self.centralwidget)
self.phoneNumberEntry.setGeometry(QtCore.QRect(20, 500, 391, 27))
self.phoneNumberEntry.setReadOnly(True)
self.phoneNumberEntry.setObjectName("phoneNumberEntry")
self.firstNameLabel = QtWidgets.QLabel(self.centralwidget)
self.firstNameLabel.setGeometry(QtCore.QRect(20, 40, 62, 17))
self.firstNameLabel.setObjectName("firstNameLabel")
self.cityLabel = QtWidgets.QLabel(self.centralwidget)
self.cityLabel.setGeometry(QtCore.QRect(20, 290, 23, 17))
self.cityLabel.setObjectName("cityLabel")
self.stateLabel = QtWidgets.QLabel(self.centralwidget)
self.stateLabel.setGeometry(QtCore.QRect(20, 350, 29, 17))
self.stateLabel.setObjectName("stateLabel")
self.zipEntry = QtWidgets.QLineEdit(self.centralwidget)
self.zipEntry.setGeometry(QtCore.QRect(20, 440, 391, 27))
self.zipEntry.setReadOnly(True)
self.zipEntry.setObjectName("zipEntry")
self.zipLabel = QtWidgets.QLabel(self.centralwidget)
self.zipLabel.setGeometry(QtCore.QRect(20, 410, 18, 17))
self.zipLabel.setObjectName("zipLabel")
self.emailEntry = QtWidgets.QLineEdit(self.centralwidget)
self.emailEntry.setGeometry(QtCore.QRect(20, 550, 391, 27))
self.emailEntry.setObjectName("emailEntry")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 530, 80, 17))
self.label.setObjectName("label")
self.confirmNewPersonButton = QtWidgets.QPushButton(self.centralwidget)
self.confirmNewPersonButton.setGeometry(QtCore.QRect(20, 590, 85, 27))
self.confirmNewPersonButton.setObjectName("confirmNewPersonButton")
self.lastNameEntry = QtWidgets.QLineEdit(self.centralwidget)
self.lastNameEntry.setEnabled(True)
self.lastNameEntry.setGeometry(QtCore.QRect(20, 130, 391, 27))
self.lastNameEntry.setText("")
self.lastNameEntry.setReadOnly(True)
self.lastNameEntry.setObjectName("lastNameEntry")
self.sortComboBox = QtWidgets.QComboBox(self.centralwidget)
self.sortComboBox.setGeometry(QtCore.QRect(500, 530, 101, 31))
self.sortComboBox.setObjectName("sortComboBox")
self.sortByLabel = QtWidgets.QLabel(self.centralwidget)
self.sortByLabel.setGeometry(QtCore.QRect(570, 500, 71, 31))
font = QtGui.QFont()
font.setPointSize(14)
self.sortByLabel.setFont(font)
self.sortByLabel.setObjectName("sortByLabel")
self.sortButton = QtWidgets.QPushButton(self.centralwidget)
self.sortButton.setGeometry(QtCore.QRect(600, 530, 111, 31))
font = QtGui.QFont()
font.setPointSize(14)
self.sortButton.setFont(font)
self.sortButton.setObjectName("sortButton")
self.addNewPersonButton = QtWidgets.QPushButton(self.centralwidget)
self.addNewPersonButton.setGeometry(QtCore.QRect(40, 10, 101, 27))
self.addNewPersonButton.setObjectName("addNewPersonButton")
self.editEntryButton = QtWidgets.QPushButton(self.centralwidget)
self.editEntryButton.setGeometry(QtCore.QRect(170, 10, 111, 27))
self.editEntryButton.setObjectName("editEntryButton")
self.confirmEditButton = QtWidgets.QPushButton(self.centralwidget)
self.confirmEditButton.setGeometry(QtCore.QRect(20, 590, 111, 27))
self.confirmEditButton.setObjectName("confirmEditButton")
self.deleteEntryButton = QtWidgets.QPushButton(self.centralwidget)
self.deleteEntryButton.setGeometry(QtCore.QRect(300, 10, 111, 27))
self.deleteEntryButton.setObjectName("deleteEntryButton")
self.sortByLabel_2 = QtWidgets.QLabel(self.centralwidget)
self.sortByLabel_2.setGeometry(QtCore.QRect(530, 570, 131, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.sortByLabel_2.setFont(font)
self.sortByLabel_2.setObjectName("sortByLabel_2")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(500, 600, 101, 31))
self.lineEdit.setObjectName("lineEdit")
self.sortButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.sortButton_2.setGeometry(QtCore.QRect(600, 600, 111, 31))
font = QtGui.QFont()
font.setPointSize(14)
self.sortButton_2.setFont(font)
self.sortButton_2.setObjectName("sortButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setEnabled(True)
self.menubar.setGeometry(QtCore.QRect(0, 0, 772, 27))
self.menubar.setObjectName("menubar")
self.menuOpen = QtWidgets.QMenu(self.menubar)
self.menuOpen.setObjectName("menuOpen")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionSave = QtWidgets.QAction(MainWindow)
self.actionSave.setObjectName("actionSave")
self.actionSave_as = QtWidgets.QAction(MainWindow)
self.actionSave_as.setObjectName("actionSave_as")
self.actionClose = QtWidgets.QAction(MainWindow)
self.actionClose.setObjectName("actionClose")
self.menuOpen.addAction(self.actionSave)
self.menuOpen.addSeparator()
self.menuOpen.addAction(self.actionSave_as)
self.menuOpen.addSeparator()
self.menuOpen.addAction(self.actionClose)
self.menubar.addAction(self.menuOpen.menuAction())
QtCore.QMetaObject.connectSlotsByName(MainWindow)
Here is my mainmenu py file:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(255, 410)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.createNewBookButton = QtWidgets.QPushButton(self.centralwidget)
self.createNewBookButton.setGeometry(QtCore.QRect(40, 220, 171, 41))
self.createNewBookButton.setObjectName("createNewBookButton")
self.createNewBookButton.clicked.connect(self.openNewBook)
self.openExistingBookButton = QtWidgets.QPushButton(self.centralwidget)
self.openExistingBookButton.setGeometry(QtCore.QRect(40, 280, 171, 41))
self.openExistingBookButton.setObjectName("openExistingBookButton")
self.quitProgramButton = QtWidgets.QPushButton(self.centralwidget)
self.quitProgramButton.setGeometry(QtCore.QRect(40, 340, 171, 41))
self.quitProgramButton.setObjectName("quitProgramButton")
self.mainImage = QtWidgets.QGraphicsView(self.centralwidget)
self.mainImage.setGeometry(QtCore.QRect(10, 10, 231, 192))
self.mainImage.setObjectName("mainImage")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def openNewBook(self):
self.popWin = QtWidgets.QMainWindow()
self.bookUI = bookPopout.BookPopout()
self.bookUI.setupUi(self.popWin)
self.popWin.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
This works great for opening up ONE extra window, which stays open while my main window is open which is what i want, but then when i go to click to make another window appear, it just replaces the one i already had up, so i can only seem to have one extra at a time.
I figure that my old window is being referenced by self.popWin , and then being replaced over and over so my question i guess is what is the best way to go about "generating" windows instead of just referencing the same window over and over.. i tried to remove the "self" part but then the window just dosent even appear when i go to create a new window so i am kind of lost
for example i would expect to be able to write something like this but im not really sure how to go about doing so
currentWindows = []
newPopWin = QtWidgets.QMainWindow()
newBookUI = BookPopout()
newBookUI.setupUi(newPopWin)
newPopWin.show()
currentWindows.append(newPopWin)
this would be ideal so i could keep track of all current open windows so i may terminate them all once i close my main menu.
Any help would be appreciated to put me on the right track, i have tried looking up solutions but lots of examples are not using code converted from a UI file.
The problem is simple, we are overwriting an attribute and at that moment the previous element is deleted, for that reason only a window. As you point out the solution is to store it in a list.
But before that, I recommend you not to write your logic in the .py generated by Qt Designer since for example let's say that you want to modify something of the design, then when generating the new .py it will erase all your logic. What PyQt recommends is to use create another file where you generate the logic adviritiendote with the message: # WARNING! All changes made in this file will be lost!. So restore the previous files I called bookPopup.py and mainmenu.py by removing the openNewBook. Following the docs recommendation we obtain the following:
main.py
from PyQt5 import QtCore, QtGui, QtWidgets
import bookPopout
import mainmenu
class BookPopoutWindow(QtWidgets.QMainWindow, bookPopout.BookPopout):
def __init__(self, parent=None):
super(BookPopoutWindow, self).__init__(parent)
self.setupUi(self)
class MainMenu(QtWidgets.QMainWindow, mainmenu.Ui_MainWindow):
def __init__(self, parent=None):
super(MainMenu, self).__init__(parent)
self.setupUi(self)
self.createNewBookButton.clicked.connect(self.openNewBook)
self.popups = []
#QtCore.pyqtSlot()
def openNewBook(self):
popWin = BookPopoutWindow()
popWin.show()
self.popups.append(popWin)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainMenu()
w.show()
sys.exit(app.exec_())
PyQt5 Opening Multiple Windows/Widgets and Closing them
import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QAction
import random
class Widget2(QWidget):
def __init__(self):
super().__init__()
w2btn = QPushButton('Another Widget ' +
str(random.randrange(5, 100, 5)), self)
w2btn.clicked.connect(self.w2btnclicked)
w2btn.resize(w2btn.sizeHint())
w2btn.move(50, 50)
def w2btnclicked(self):
print("Widget 2 btn clicked")
class Widget1(QWidget):
def __init__(self):
super().__init__()
self.start = 50
self.end = 50
quit = QAction("Quit", self)
quit.triggered.connect(self.close)
addbtn = QPushButton('Add Window', self)
addbtn.clicked.connect(self.addbtnclicked)
addbtn.resize(addbtn.sizeHint())
addbtn.move(50, 50)
quitbtn = QPushButton('Quit', self)
quitbtn.clicked.connect(QApplication.instance().quit)
quitbtn.resize(quitbtn.sizeHint())
quitbtn.move(50, 100)
self.popups = []
def addbtnclicked(self):
print("Add Button Clicked!!")
wdgt2 = Widget2()
wdgt2.show()
if self.start > 1600:
self.start = 50
self.end = self.end + 250
wdgt2.setGeometry(self.start, self.end, 200, 200)
self.popups.append(wdgt2)
self.start = self.start + 250
def closeEvent(self, event):
print("In Close Event")
QApplication.closeAllWindows()
def main():
app = QApplication(sys.argv)
ex = Widget1()
ex.show()
ex.setGeometry(800, 600, 200, 200)
sys.exit(app.exec_())
if __name__ == '__main__':
main()