PyQt5 QPushbutton to store ariables Python - python

Using PyQt5 QPlaintext to store variables. Example AO = userinput?
What I have so far minus the GUI setup. I need to store it as simple variables so i can output to a DOCX. I know I have to manually user input for every QPlaintextbox available. would I use the getText(), link it to the push button to generate the variables. Thank you for your time.
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(1340, 922)
self.OPORD = QtWidgets.QTabWidget(Dialog)
self.OPORD.setGeometry(QtCore.QRect(0, 0, 1231, 991))
self.OPORD.setTabBarAutoHide(False)
self.OPORD.setObjectName("OPORD")
self.tab_4 = QtWidgets.QWidget()
self.tab_4.setObjectName("tab_4")
self.label = QtWidgets.QLabel(self.tab_4)
self.label.setGeometry(QtCore.QRect(280, 240, 311, 331))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("WAATS_Final.png"))
self.label.setObjectName("label")
self.OPORD.addTab(self.tab_4, "")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.ORDERS = QtWidgets.QStackedWidget(self.tab)
self.ORDERS.setGeometry(QtCore.QRect(0, 0, 1141, 1080))
self.ORDERS.setMaximumSize(QtCore.QSize(1920, 1080))
self.ORDERS.setObjectName("ORDERS")
self.stackedWidgetPage1 = QtWidgets.QWidget()
self.stackedWidgetPage1.setObjectName("stackedWidgetPage1")
self.WAATS4 = QtWidgets.QCheckBox(self.stackedWidgetPage1)
self.WAATS4.setGeometry(QtCore.QRect(390, 50, 141, 20))
self.WAATS4.setMaximumSize(QtCore.QSize(1920, 1080))
self.WAATS4.setObjectName("WAATS4")
self.label_2 = QtWidgets.QLabel(self.stackedWidgetPage1)
self.label_2.setGeometry(QtCore.QRect(0, 70, 271, 21))
self.label_2.setMaximumSize(QtCore.QSize(1920, 1080))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_9 = QtWidgets.QLabel(self.stackedWidgetPage1)
self.label_9.setGeometry(QtCore.QRect(240, 0, 181, 21))
self.label_9.setMaximumSize(QtCore.QSize(1920, 1080))
self.label_9.setText("")
self.label_9.setObjectName("label_9")
self.num = QtWidgets.QLineEdit(self.stackedWidgetPage1)
self.num.setGeometry(QtCore.QRect(0, 30, 181, 31))
self.num.setMaximumSize(QtCore.QSize(1920, 1080))
self.num.setObjectName("num")
self.aoi = QtWidgets.QPlainTextEdit(self.stackedWidgetPage1)
self.aoi.setGeometry(QtCore.QRect(0, 110, 431, 71))
self.aoi.setMaximumSize(QtCore.QSize(1920, 1080))
self.aoi.setObjectName("aoi")
self.COPORD = QtWidgets.QPushButton(self.widget)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.OPORD.setTabText(self.OPORD.indexOf(self.tab_4), _translate("Dialog", "Welcome"))
self.WAATS4.setText(_translate("Dialog", "TASS"))
self.label_2.setText(_translate("Dialog", "Situation"))
self.num.setPlaceholderText(_translate("Dialog", "##-##"))
self.aoi.setPlainText(_translate("Dialog", "?"))
self.aoi.setPlaceholderText(_translate("Dialog", "AI"))
self.label_3.setText(_translate("Dialog", "Area of Interest"))
self.AO.setText(_translate("Dialog", "Area of Operation"))
self.ao.setPlaceholderText(_translate("Dialog", "AO"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())

as I see on your code, to get string from the QLineEdit you can use this code.
self.num.text()
if you print that code, it will automatically print the inputed string of QLineEdit.
you can add a QPushButton then make a signal slot to do that as a form in commonly use.
like these:
self.button = QPushButton()
self.button.clicked.connect(self.getLineEdit)
def getLineEdit(self):
lineEditText = self.num.text()
print(lineEditText)

Related

turning Sqlite records into python variables

This is my code. The code in email_compare() is my problem. I want it to take the input from the email box and compare it to the emails in my Sqlite database using a linear search. The problem is that though the input is exactly the same it won't output that it is correct. I have had to delete certain parts of my code due to having more code than writing so if it is required I will right back to say what I have deleted. Also I am not sure if you can turn Sqlite records into variables that can actually be compared so if there is another way of doing this then I would greatly appreciate being told how to do it in that method instead of what I am trying to do now.
from PyQt6 import QtCore, QtGui, QtWidgets
import sqlite3
from SIGNUP import Ui_MainWindow
class Ui_Dialog(object):
def openWindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.window)
Dialog.close()
self.window.show()
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(480, 620)
Dialog.setStyleSheet("background: rgb(64, 64, 64)")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(210, 60, 200, 50))
self.label.setStyleSheet("color:rgb(255, 255, 255);\n"
"font-size: 28pt;")
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(60, 150, 91, 41))
self.label_2.setStyleSheet("font: 23pt; color: rgb(255, 0, 0)")
self.label_2.setObjectName("label_2")
self.email = QtWidgets.QLineEdit(Dialog)
self.email.setGeometry(QtCore.QRect(60, 190, 251, 41))
self.email.setStyleSheet("color: rgb(255, 255, 255)")
self.email.setObjectName("email")
self.label_3 = QtWidgets.QLabel(Dialog)
self.label_3.setGeometry(QtCore.QRect(60, 260, 101, 41))
self.label_3.setStyleSheet("font: 23pt; color: rgb(255, 0, 0)")
self.label_3.setObjectName("label_3")
self.password = QtWidgets.QLineEdit(Dialog)
self.password.setGeometry(QtCore.QRect(60, 300, 251, 41))
self.password.setStyleSheet("color: rgb(255, 255, 255)")
self.password.setText("")
self.password.setObjectName("password")
self.loginbutton = QtWidgets.QPushButton(Dialog)
self.loginbutton.setGeometry(QtCore.QRect(330, 390, 100, 32))
self.loginbutton.setStyleSheet("color: rgb(255, 0, 0)")
self.loginbutton.setObjectName("loginbutton")
self.loginbutton_2 = QtWidgets.QPushButton(Dialog)
self.loginbutton_2.setGeometry(QtCore.QRect(50, 390, 100, 32))
self.loginbutton_2.setStyleSheet("background-color: rgb(0, 255, 212);")
self.loginbutton_2.setObjectName("loginbutton_2")
self.loginbutton_2.clicked.connect(self.openWindow)
self.loginbutton.clicked.connect(self.show_password)
self.loginbutton.clicked.connect(self.email_compare)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Login"))
self.label_2.setText(_translate("Dialog", "Email"))
self.label_3.setText(_translate("Dialog", "Password"))
self.loginbutton.setText(_translate("Dialog", "Login!"))
self.loginbutton_2.setText(_translate("Dialog", "Sign up!"))
# this is my problem
def email_compare(self):
count = 0
check = self.email.text()
check = "('"+check+"',)"
conn = sqlite3.connect("U+P.db")
c = conn.cursor()
c.execute("SELECT email FROM Details")
compare = c.fetchall()
print(compare)
print(check)
right = False
while count < len(compare) and right == False:
print(compare[count])
if compare[count] == check:
print("Correct email")
right = True
else:
count = count + 1
conn.close()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec())

How to output to the application itself

I wrote a program that adds two numbers entered by the user into QLineEdit.
The result of the addition is output to the console in Pycharm.
How to make the result output to the application itself? (so that the user can see the answer).
I guess that there is some special method for this, but what is it?
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Probnei(object):
def setupUi(self, Probnei):
Probnei.setObjectName("Probnei")
Probnei.resize(439, 309)
self.centralwidget = QtWidgets.QWidget(Probnei)
self.centralwidget.setObjectName("centralwidget")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(90, 140, 214, 22))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.layoutWidget)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.Second_n = QtWidgets.QLineEdit(self.layoutWidget)
self.Second_n.setObjectName("Second_n")
self.horizontalLayout_2.addWidget(self.Second_n)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(360, 210, 75, 23))
self.pushButton.setObjectName("pushButton")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(90, 60, 214, 22))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.widget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.First_n = QtWidgets.QLineEdit(self.widget)
self.First_n.setObjectName("First_n")
self.horizontalLayout.addWidget(self.First_n)
Probnei.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(Probnei)
self.menubar.setGeometry(QtCore.QRect(0, 0, 439, 21))
self.menubar.setObjectName("menubar")
Probnei.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(Probnei)
self.statusbar.setObjectName("statusbar")
Probnei.setStatusBar(self.statusbar)
self.retranslateUi(Probnei)
QtCore.QMetaObject.connectSlotsByName(Probnei)
def retranslateUi(self, Probnei):
_translate = QtCore.QCoreApplication.translate
Probnei.setWindowTitle(_translate("Probnei", "MainWindow"))
self.label_2.setText(_translate("Probnei", "Второе число:"))
self.pushButton.setText(_translate("Probnei", "Посчитать"))
self.label.setText(_translate("Probnei", "Первое число:"))
self.pushButton.clicked.connect(self.cal)
def cal(self, Probnei):
val_1 = self.First_n.text()
val_2 = self.Second_n.text()
res = int(val_1) + int(val_2)
print(res)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Probnei = QtWidgets.QMainWindow()
ui = Ui_Probnei()
ui.setupUi(Probnei)
Probnei.show()
sys.exit(app.exec_())
How to make the result output to the application itself?
It's not magic. You just have to create a widget that will display the answer. Then set the text on that widget when you do the calculation.
self.answerLabel = QtWidgets.QLabel(self.widget)
def cal(self, Probnei):
val_1 = self.First_n.text()
val_2 = self.Second_n.text()
res = int(val_1) + int(val_2)
self.answerLabel.setText(res)

Pushbutton on second Dialog window does not work

I have created two Windows in QT designer. The first one has two pushButtons, each one calls the Dialog window. I am just starting to write the code for this but I could not go further when I tested the pushbutton on the dialog window. I have just connected to a function which calls print ("test."), Just to see the PB works, but it doesn't work.what I am missing?
See the main code below:
from PyQt5 import QtWidgets
from Main_VIEW import Ui_MainWindow # this is the main window
from Dialog_sa import Ui_Dialog # dialog window
from Test4 import currentAccount,savingAccount # this has all functions and calculations
import sys
class ApplicationWindow(QtWidgets.QMainWindow,Ui_MainWindow,Ui_Dialog,QtWidgets.QDialog):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
#self.diag_ui = Ui_Dialog()
#self.diag_ui.setupUi(self)
self.ui.pb_ca.clicked.connect(self.open_dialog_ca)
def open_dialog_ca(self):
#global dialog
dialog = QtWidgets.QDialog()
dialog.ui = Ui_Dialog()
dialog.ui.setupUi(dialog)
dialog.exec_()
dialog.show()
account = currentAccount()
dialog.value_str = dialog.ui.le_b.text()
dialog.ui.pb_d.clicked.connect(self.print_sel) # this is the line of code that doesn't work
def print_sel(self):
print("this app")
def main():
app = QtWidgets.QApplication(sys.argv)
application = ApplicationWindow()
application.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
this is Dialog_sa
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Dialog_sa.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# 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_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(301, 142)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(-70, 110, 341, 32))
font = QtGui.QFont()
font.setFamily("Calibri Light")
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
self.buttonBox.setFont(font)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.le_b = QtWidgets.QLineEdit(Dialog)
self.le_b.setGeometry(QtCore.QRect(50, 40, 131, 61))
font = QtGui.QFont()
font.setFamily("Calibri Light")
font.setPointSize(18)
self.le_b.setFont(font)
self.le_b.setObjectName("le_b")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(50, 20, 47, 13))
font = QtGui.QFont()
font.setFamily("Calibri")
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.pb_w = QtWidgets.QPushButton(Dialog)
self.pb_w.setGeometry(QtCore.QRect(190, 80, 75, 23))
font = QtGui.QFont()
font.setFamily("Calibri Light")
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
self.pb_w.setFont(font)
self.pb_w.setObjectName("pb_w")
self.pb_d = QtWidgets.QPushButton(Dialog)
self.pb_d.setGeometry(QtCore.QRect(190, 40, 75, 23))
font = QtGui.QFont()
font.setFamily("Calibri Light")
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
self.pb_d.setFont(font)
self.pb_d.setObjectName("pb_d")
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Balance"))
self.pb_w.setText(_translate("Dialog", "Withdraw"))
self.pb_d.setText(_translate("Dialog", "Deposit"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
This is MAIN_VIEW
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Main_VIEW.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# 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(259, 147)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pb_ca = QtWidgets.QPushButton(self.centralwidget)
self.pb_ca.setGeometry(QtCore.QRect(50, 10, 171, 41))
font = QtGui.QFont()
font.setFamily("Calibri Light")
font.setPointSize(14)
font.setBold(False)
font.setWeight(50)
self.pb_ca.setFont(font)
self.pb_ca.setObjectName("pb_ca")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(50, 60, 171, 41))
font = QtGui.QFont()
font.setFamily("Calibri Light")
font.setPointSize(14)
font.setBold(False)
font.setWeight(50)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 259, 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 retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pb_ca.setText(_translate("MainWindow", "Current Account"))
self.pushButton_2.setText(_translate("MainWindow", "Saving Account"))
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_())

Cannot open second window, Event loop already running - PyQt5 [duplicate]

This question already has answers here:
Close and Open new Window PYQT5
(2 answers)
Closed 3 years ago.
I am currently trying to get a button press in one qt window to open a second window which is in another python file. My layout code was generated by converting a ui made in Qt designer to python.
My select feature code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'movie_design.ui'
#
# Created by: PyQt5 UI code generator 5.14.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
import add_entry
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(597, 522)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(160, 40, 291, 51))
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(False)
font.setUnderline(True)
font.setWeight(50)
font.setStrikeOut(False)
font.setKerning(True)
self.label.setFont(font)
self.label.setLineWidth(1)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(170, 110, 261, 91))
font = QtGui.QFont()
font.setPointSize(12)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(170, 230, 261, 91))
font = QtGui.QFont()
font.setPointSize(12)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(170, 350, 261, 91))
font = QtGui.QFont()
font.setPointSize(12)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 597, 26))
self.menubar.setObjectName("menubar")
self.menuMenu = QtWidgets.QMenu(self.menubar)
self.menuMenu.setObjectName("menuMenu")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuMenu.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton_2.clicked.connect(self.add)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Movie Database Program"))
self.pushButton.setText(_translate("MainWindow", "Delete Record"))
self.pushButton_2.setText(_translate("MainWindow", "Add Record"))
self.pushButton_3.setText(_translate("MainWindow", "Search Records"))
self.menuMenu.setTitle(_translate("MainWindow", "Menu"))
def add(self):
add_entry.add_entry()
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
app.exec_()
if __name__ == "__main__":
main()
As you can see, I have created a button that when pressed calls the function in the program "add_entry.py"
Add entry.py code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'add_entry.ui'
#
# Created by: PyQt5 UI code generator 5.14.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(555, 418)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(10, 30, 533, 28))
font = QtGui.QFont()
font.setPointSize(14)
self.label.setFont(font)
self.label.setTextFormat(QtCore.Qt.AutoText)
self.label.setScaledContents(False)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.formLayoutWidget = QtWidgets.QWidget(Dialog)
self.formLayoutWidget.setGeometry(QtCore.QRect(30, 70, 501, 321))
self.formLayoutWidget.setObjectName("formLayoutWidget")
self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
self.formLayout.setContentsMargins(0, 0, 0, 0)
self.formLayout.setObjectName("formLayout")
self.label_2 = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label_2)
self.MovieTitle = QtWidgets.QLineEdit(self.formLayoutWidget)
self.MovieTitle.setObjectName("MovieTitle")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.MovieTitle)
self.label_3 = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_3)
self.MovieStudio = QtWidgets.QLineEdit(self.formLayoutWidget)
self.MovieStudio.setObjectName("MovieStudio")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.MovieStudio)
self.label_4 = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_4)
self.dateEdit = QtWidgets.QDateEdit(self.formLayoutWidget)
self.dateEdit.setObjectName("dateEdit")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.dateEdit)
self.label_5 = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.label_5)
self.ProductionCost = QtWidgets.QSpinBox(self.formLayoutWidget)
self.ProductionCost.setMaximum(1000000000)
self.ProductionCost.setObjectName("ProductionCost")
self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.ProductionCost)
self.label_6 = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.label_6.setFont(font)
self.label_6.setObjectName("label_6")
self.formLayout.setWidget(4, QtWidgets.QFormLayout.LabelRole, self.label_6)
self.BoxOfficeRevenue = QtWidgets.QSpinBox(self.formLayoutWidget)
self.BoxOfficeRevenue.setMaximum(1000000000)
self.BoxOfficeRevenue.setObjectName("BoxOfficeRevenue")
self.formLayout.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.BoxOfficeRevenue)
self.label_7 = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.label_7.setFont(font)
self.label_7.setObjectName("label_7")
self.formLayout.setWidget(5, QtWidgets.QFormLayout.LabelRole, self.label_7)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.No = QtWidgets.QRadioButton(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(10)
self.No.setFont(font)
self.No.setObjectName("No")
self.horizontalLayout.addWidget(self.No)
self.Yes = QtWidgets.QRadioButton(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(10)
self.Yes.setFont(font)
self.Yes.setObjectName("Yes")
self.horizontalLayout.addWidget(self.Yes)
self.formLayout.setLayout(5, QtWidgets.QFormLayout.FieldRole, self.horizontalLayout)
self.label_8 = QtWidgets.QLabel(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(12)
self.label_8.setFont(font)
self.label_8.setObjectName("label_8")
self.formLayout.setWidget(6, QtWidgets.QFormLayout.LabelRole, self.label_8)
self.classificationBox = QtWidgets.QComboBox(self.formLayoutWidget)
font = QtGui.QFont()
font.setPointSize(10)
self.classificationBox.setFont(font)
self.classificationBox.setObjectName("classificationBox")
self.classificationBox.addItem("")
self.classificationBox.addItem("")
self.classificationBox.addItem("")
self.classificationBox.addItem("")
self.classificationBox.addItem("")
self.formLayout.setWidget(6, QtWidgets.QFormLayout.FieldRole, self.classificationBox)
self.pushButton = QtWidgets.QPushButton(self.formLayoutWidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
self.pushButton.setSizePolicy(sizePolicy)
self.pushButton.setObjectName("pushButton")
self.formLayout.setWidget(7, QtWidgets.QFormLayout.SpanningRole, self.pushButton)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "<html><head/><body><p><span style=\" font-size:12pt; text-decoration: underline;\">Add Entry</span></p></body></html>"))
self.label_2.setText(_translate("Dialog", "Movie Title"))
self.label_3.setText(_translate("Dialog", "Movie Studio"))
self.label_4.setText(_translate("Dialog", "Release Date"))
self.label_5.setText(_translate("Dialog", "Production Cost (m$)"))
self.label_6.setText(_translate("Dialog", "Box Office Revenue (m$)"))
self.label_7.setText(_translate("Dialog", "Have you seen this movie?"))
self.No.setText(_translate("Dialog", "Yes"))
self.Yes.setText(_translate("Dialog", "No"))
self.label_8.setText(_translate("Dialog", "Classification (Age Rating)"))
self.classificationBox.setItemText(0, _translate("Dialog", "U"))
self.classificationBox.setItemText(1, _translate("Dialog", "PG"))
self.classificationBox.setItemText(2, _translate("Dialog", "12"))
self.classificationBox.setItemText(3, _translate("Dialog", "15"))
self.classificationBox.setItemText(4, _translate("Dialog", "18"))
self.pushButton.setText(_translate("Dialog", "Add Entry"))
def add_entry():
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
app.exec_()
if __name__ == "__main__":
add_entry()
The issue is that the when the button is pressed, the program crashes, telling me that "Event loop already running". How do I fix this?
Qt, like many GUI frameworks, is based on an event loop which is started with app.exec_() and then waits for events to react to (both from the user or the system).
For each Qt program there should always be a single and only QApplication instance running its event loop.
With your implementation, each time you press the button, you're trying to create and launch a new QApplication, and Qt will not allow you for the reason above.
Nonetheless, the problem with your code is somewhere else, as you are using the generated UI code in the wrong way: those files should never be used as a basis to create your program code (in fact, you should always leave them as they are).
I suggest you to carefully read the official documentation about using Designer to understand how to use those files.
Regenerate the UI files with pyuic, and then create a new script that will probably contain something similar to this:
from PyQt5 import QtWidgets
from ui_mainwindow import Ui_MainWindow
from ui_dialog import Ui_Dialog
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton_2.clicked.connect(self.add)
def add(self):
dialog = Dialog(self)
dialog.exec_()
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
In this example I used the third method (multiple inheritance approach) explained in the documentation given above.

How do I pass parameters from the GUI PyQt5 widgets to the python code I have working through a button

This is something I'm doing for work, to speed up a process with the software we use. I'm trying to change a text file by using different regex search & replace functions. I have that working, and I have the GUI the way I want it.
import MainCode
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(786, 73)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(1)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
MainWindow.setMinimumSize(QtCore.QSize(786, 73))
MainWindow.setWindowOpacity(1.0)
MainWindow.setDockNestingEnabled(True)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.StyleGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.StyleGroupBox.setGeometry(QtCore.QRect(0, 10, 101, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.StyleGroupBox.setFont(font)
self.StyleGroupBox.setFocusPolicy(QtCore.Qt.NoFocus)
self.StyleGroupBox.setCheckable(True)
self.StyleGroupBox.setChecked(False)
self.StyleGroupBox.setObjectName("StyleGroupBox")
self.StyleComboBox = QtWidgets.QComboBox(self.StyleGroupBox)
self.StyleComboBox.setGeometry(QtCore.QRect(10, 20, 81, 31))
self.StyleComboBox.setMinimumSize(QtCore.QSize(69, 0))
self.StyleComboBox.setSizeIncrement(QtCore.QSize(5, 5))
self.StyleComboBox.setFocusPolicy(QtCore.Qt.ClickFocus)
self.StyleComboBox.setMaxCount(100)
self.StyleComboBox.setSizeAdjustPolicy(QtWidgets.QComboBox.AdjustToContentsOnFirstShow)
self.StyleComboBox.setMinimumContentsLength(1)
self.StyleComboBox.setDuplicatesEnabled(True)
self.StyleComboBox.setObjectName("StyleComboBox")
self.StyleComboBox.addItem("")
self.StyleComboBox.addItem("")
self.StyleComboBox.addItem("")
self.StyleComboBox.addItem("")
self.StyleComboBox.addItem("")
self.FontGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.FontGroupBox.setEnabled(True)
self.FontGroupBox.setGeometry(QtCore.QRect(100, 10, 81, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.FontGroupBox.setFont(font)
self.FontGroupBox.setFocusPolicy(QtCore.Qt.NoFocus)
self.FontGroupBox.setCheckable(True)
self.FontGroupBox.setChecked(False)
self.FontGroupBox.setObjectName("FontGroupBox")
self.FontSpinBox = QtWidgets.QSpinBox(self.FontGroupBox)
self.FontSpinBox.setGeometry(QtCore.QRect(10, 20, 61, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.FontSpinBox.setFont(font)
self.FontSpinBox.setFocusPolicy(QtCore.Qt.StrongFocus)
self.FontSpinBox.setMinimum(8)
self.FontSpinBox.setMaximum(200)
self.FontSpinBox.setProperty("value", 24)
self.FontSpinBox.setObjectName("FontSpinBox")
self.OpacityGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.OpacityGroupBox.setEnabled(True)
self.OpacityGroupBox.setGeometry(QtCore.QRect(180, 10, 81, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.OpacityGroupBox.setFont(font)
self.OpacityGroupBox.setFocusPolicy(QtCore.Qt.ClickFocus)
self.OpacityGroupBox.setCheckable(True)
self.OpacityGroupBox.setChecked(False)
self.OpacityGroupBox.setObjectName("OpacityGroupBox")
self.OpacityDoubleSpinBox = QtWidgets.QDoubleSpinBox(self.OpacityGroupBox)
self.OpacityDoubleSpinBox.setGeometry(QtCore.QRect(10, 20, 61, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.OpacityDoubleSpinBox.setFont(font)
self.OpacityDoubleSpinBox.setToolTip("")
self.OpacityDoubleSpinBox.setDecimals(2)
self.OpacityDoubleSpinBox.setMaximum(1.0)
self.OpacityDoubleSpinBox.setSingleStep(0.01)
self.OpacityDoubleSpinBox.setProperty("value", 1.0)
self.OpacityDoubleSpinBox.setObjectName("OpacityDoubleSpinBox")
self.NextPageGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.NextPageGroupBox.setEnabled(True)
self.NextPageGroupBox.setGeometry(QtCore.QRect(260, 10, 91, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.NextPageGroupBox.setFont(font)
self.NextPageGroupBox.setFocusPolicy(QtCore.Qt.ClickFocus)
self.NextPageGroupBox.setCheckable(True)
self.NextPageGroupBox.setChecked(False)
self.NextPageGroupBox.setObjectName("NextPageGroupBox")
self.NextPageSpinBox = QtWidgets.QSpinBox(self.NextPageGroupBox)
self.NextPageSpinBox.setGeometry(QtCore.QRect(10, 20, 81, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.NextPageSpinBox.setFont(font)
self.NextPageSpinBox.setFocusPolicy(QtCore.Qt.StrongFocus)
self.NextPageSpinBox.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
self.NextPageSpinBox.setButtonSymbols(QtWidgets.QAbstractSpinBox.PlusMinus)
self.NextPageSpinBox.setAccelerated(True)
self.NextPageSpinBox.setMaximum(999999999)
self.NextPageSpinBox.setDisplayIntegerBase(10)
self.NextPageSpinBox.setObjectName("NextPageSpinBox")
self.ZindexGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.ZindexGroupBox.setEnabled(True)
self.ZindexGroupBox.setGeometry(QtCore.QRect(350, 10, 71, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.ZindexGroupBox.setFont(font)
self.ZindexGroupBox.setFocusPolicy(QtCore.Qt.ClickFocus)
self.ZindexGroupBox.setCheckable(True)
self.ZindexGroupBox.setChecked(False)
self.ZindexGroupBox.setObjectName("ZindexGroupBox")
self.ZindexSpinBox = QtWidgets.QSpinBox(self.ZindexGroupBox)
self.ZindexSpinBox.setGeometry(QtCore.QRect(10, 20, 51, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.ZindexSpinBox.setFont(font)
self.ZindexSpinBox.setObjectName("ZindexSpinBox")
self.AddChangeFrame = QtWidgets.QFrame(self.centralwidget)
self.AddChangeFrame.setEnabled(True)
self.AddChangeFrame.setGeometry(QtCore.QRect(420, 0, 351, 71))
font = QtGui.QFont()
font.setPointSize(8)
self.AddChangeFrame.setFont(font)
self.AddChangeFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.AddChangeFrame.setFrameShadow(QtWidgets.QFrame.Raised)
self.AddChangeFrame.setObjectName("AddChangeFrame")
self.NewButtonsSpinBox = QtWidgets.QSpinBox(self.AddChangeFrame)
self.NewButtonsSpinBox.setGeometry(QtCore.QRect(10, 20, 41, 31))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(False)
font.setWeight(50)
self.NewButtonsSpinBox.setFont(font)
self.NewButtonsSpinBox.setMouseTracking(True)
self.NewButtonsSpinBox.setAlignment(QtCore.Qt.AlignCenter)
self.NewButtonsSpinBox.setMinimum(1)
self.NewButtonsSpinBox.setObjectName("NewButtonsSpinBox")
self.NewButtonsRadio = QtWidgets.QRadioButton(self.AddChangeFrame)
self.NewButtonsRadio.setGeometry(QtCore.QRect(0, 0, 161, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.NewButtonsRadio.setFont(font)
self.NewButtonsRadio.setFocusPolicy(QtCore.Qt.ClickFocus)
self.NewButtonsRadio.setObjectName("NewButtonsRadio")
self.StartItemLabel = QtWidgets.QLabel(self.AddChangeFrame)
self.StartItemLabel.setGeometry(QtCore.QRect(70, 20, 61, 21))
self.StartItemLabel.setObjectName("StartItemLabel")
self.EndingItemLabel = QtWidgets.QLabel(self.AddChangeFrame)
self.EndingItemLabel.setGeometry(QtCore.QRect(160, 20, 51, 20))
self.EndingItemLabel.setObjectName("EndingItemLabel")
self.StartItemNumberSpinBox = QtWidgets.QSpinBox(self.AddChangeFrame)
self.StartItemNumberSpinBox.setGeometry(QtCore.QRect(60, 40, 81, 22))
self.StartItemNumberSpinBox.setObjectName("StartItemNumberSpinBox")
self.EndItemNumberSpinBox = QtWidgets.QSpinBox(self.AddChangeFrame)
self.EndItemNumberSpinBox.setGeometry(QtCore.QRect(150, 40, 81, 22))
self.EndItemNumberSpinBox.setObjectName("EndItemNumberSpinBox")
self.ChangeRadioButton = QtWidgets.QRadioButton(self.AddChangeFrame)
self.ChangeRadioButton.setGeometry(QtCore.QRect(170, 0, 181, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.ChangeRadioButton.setFont(font)
self.ChangeRadioButton.setChecked(True)
self.ChangeRadioButton.setObjectName("ChangeRadioButton")
self.SubmitChangesButton = QtWidgets.QPushButton(self.centralwidget)
self.SubmitChangesButton.setGeometry(QtCore.QRect(660, 30, 131, 41))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(11)
font.setBold(True)
font.setItalic(True)
font.setWeight(75)
font.setKerning(True)
self.SubmitChangesButton.setFont(font)
self.SubmitChangesButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.SubmitChangesButton.setAutoFillBackground(True)
self.SubmitChangesButton.setObjectName("SubmitChangesButton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Simphony Button Editor"))
self.StyleGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Changes the style & color of the button using the drop-down menu.</p></body></html>"))
self.StyleGroupBox.setWhatsThis(_translate("MainWindow", "<html><head/><body><p>Changes style & color of button</p></body></html>"))
self.StyleGroupBox.setTitle(_translate("MainWindow", "Color/ Style"))
self.StyleComboBox.setItemText(0, _translate("MainWindow", "Button1"))
self.StyleComboBox.setItemText(1, _translate("MainWindow", "Button2"))
self.StyleComboBox.setItemText(2, _translate("MainWindow", "Button3"))
self.StyleComboBox.setItemText(3, _translate("MainWindow", "Button4"))
self.StyleComboBox.setItemText(4, _translate("MainWindow", "Button5"))
self.FontGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Changes the font size the button text.</p></body></html>"))
self.FontGroupBox.setTitle(_translate("MainWindow", "Font Size"))
self.OpacityGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Changes the percentage Opacity (visibility) of the button.</p></body></html>"))
self.OpacityGroupBox.setTitle(_translate("MainWindow", "Opacity"))
self.NextPageGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Assigns the next menu page to jump to after button completion. </p></body></html>"))
self.NextPageGroupBox.setTitle(_translate("MainWindow", "Next Page"))
self.ZindexGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Changes the visibility layer of the button. If a button is set to 1 and another set to 2 in the same spot then you would only see the button with Z index 2.</p></body></html>"))
self.ZindexGroupBox.setTitle(_translate("MainWindow", "Z Index"))
self.NewButtonsRadio.setText(_translate("MainWindow", "Create New Buttons"))
self.StartItemLabel.setText(_translate("MainWindow", "Start Item#"))
self.EndingItemLabel.setText(_translate("MainWindow", "End Item#"))
self.StartItemNumberSpinBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Enter Item number. Items must be in sequintial order if adding multiple buttons.</p></body></html>"))
self.EndItemNumberSpinBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Items must be in sequintial order if adding multiple buttons.</p></body></html>"))
self.ChangeRadioButton.setText(_translate("MainWindow", "Update Copied Buttons"))
self.SubmitChangesButton.setText(_translate("MainWindow", "Make Changes"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.SubmitChangesButton.clicked.connect(self.on_clicked)
#QtCore.pyqtSlot()
def on_clicked(self):
if self.FontGroupBox.isChecked() is True:
print('Font Size = ' + self.FontSpinBox.value())
if self.StyleGroupBox.isChecked() is True:
print(self.StyleComboBox.Text())
if self.OpacityGroupBox.isChecked() is True:
print('Opacity% = ' + self.OpacityDoubleSpinBox.value())
if self.NextPageGroupBox.isChecked() is True:
print('Next Page = ' + self.NextPageSpinBox.value())
if self.ZindexGroupBox.isChecked() is True:
print('Z Index = ' + self.ZindexSpinBox.value())
if __name__ == "__main__":
import sys
import MainCode
import MyDesign
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindo`enter code here`w()
ui.setupUi(MainWindow)
MainWindow.show()
print(Ui_MainWindow)
sys.exit(app.exec_())
I just need to be able to pull the results from the input that has been entered, if the frames are enabled(checked) when I click the submit buttons. Output should be true or false for checkboxes and radio buttons and integers, a double, and a string for the rest. I'm sure I'm missing something simple.
We created a class MainWindow for the top-level window.
Create an instance of it and display the widget on the screen.
w = MainWindow()
w.show()
Added the ability to get the selected item in the QComboBox drop-down list.
Corrected the output format in the on_clicked method.
#import MainCode
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(786, 73)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(1)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
MainWindow.setMinimumSize(QtCore.QSize(786, 73))
MainWindow.setWindowOpacity(1.0)
MainWindow.setDockNestingEnabled(True)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.StyleGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.StyleGroupBox.setGeometry(QtCore.QRect(0, 10, 101, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.StyleGroupBox.setFont(font)
self.StyleGroupBox.setFocusPolicy(QtCore.Qt.NoFocus)
self.StyleGroupBox.setCheckable(True)
self.StyleGroupBox.setChecked(False)
self.StyleGroupBox.setObjectName("StyleGroupBox")
self.StyleComboBox = QtWidgets.QComboBox(self.StyleGroupBox)
self.StyleComboBox.setGeometry(QtCore.QRect(10, 20, 81, 31))
self.StyleComboBox.setMinimumSize(QtCore.QSize(69, 0))
self.StyleComboBox.setSizeIncrement(QtCore.QSize(5, 5))
self.StyleComboBox.setFocusPolicy(QtCore.Qt.ClickFocus)
self.StyleComboBox.setMaxCount(100)
self.StyleComboBox.setSizeAdjustPolicy(QtWidgets.QComboBox.AdjustToContentsOnFirstShow)
self.StyleComboBox.setMinimumContentsLength(1)
self.StyleComboBox.setDuplicatesEnabled(True)
self.StyleComboBox.setObjectName("StyleComboBox")
self.StyleComboBox.addItem("")
self.StyleComboBox.addItem("")
self.StyleComboBox.addItem("")
self.StyleComboBox.addItem("")
self.StyleComboBox.addItem("")
self.FontGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.FontGroupBox.setEnabled(True)
self.FontGroupBox.setGeometry(QtCore.QRect(100, 10, 81, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.FontGroupBox.setFont(font)
self.FontGroupBox.setFocusPolicy(QtCore.Qt.NoFocus)
self.FontGroupBox.setCheckable(True)
self.FontGroupBox.setChecked(False)
self.FontGroupBox.setObjectName("FontGroupBox")
self.FontSpinBox = QtWidgets.QSpinBox(self.FontGroupBox)
self.FontSpinBox.setGeometry(QtCore.QRect(10, 20, 61, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.FontSpinBox.setFont(font)
self.FontSpinBox.setFocusPolicy(QtCore.Qt.StrongFocus)
self.FontSpinBox.setMinimum(8)
self.FontSpinBox.setMaximum(200)
self.FontSpinBox.setProperty("value", 24)
self.FontSpinBox.setObjectName("FontSpinBox")
self.OpacityGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.OpacityGroupBox.setEnabled(True)
self.OpacityGroupBox.setGeometry(QtCore.QRect(180, 10, 81, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.OpacityGroupBox.setFont(font)
self.OpacityGroupBox.setFocusPolicy(QtCore.Qt.ClickFocus)
self.OpacityGroupBox.setCheckable(True)
self.OpacityGroupBox.setChecked(False)
self.OpacityGroupBox.setObjectName("OpacityGroupBox")
self.OpacityDoubleSpinBox = QtWidgets.QDoubleSpinBox(self.OpacityGroupBox)
self.OpacityDoubleSpinBox.setGeometry(QtCore.QRect(10, 20, 61, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.OpacityDoubleSpinBox.setFont(font)
self.OpacityDoubleSpinBox.setToolTip("")
self.OpacityDoubleSpinBox.setDecimals(2)
self.OpacityDoubleSpinBox.setMaximum(1.0)
self.OpacityDoubleSpinBox.setSingleStep(0.01)
self.OpacityDoubleSpinBox.setProperty("value", 1.0)
self.OpacityDoubleSpinBox.setObjectName("OpacityDoubleSpinBox")
self.NextPageGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.NextPageGroupBox.setEnabled(True)
self.NextPageGroupBox.setGeometry(QtCore.QRect(260, 10, 91, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.NextPageGroupBox.setFont(font)
self.NextPageGroupBox.setFocusPolicy(QtCore.Qt.ClickFocus)
self.NextPageGroupBox.setCheckable(True)
self.NextPageGroupBox.setChecked(False)
self.NextPageGroupBox.setObjectName("NextPageGroupBox")
self.NextPageSpinBox = QtWidgets.QSpinBox(self.NextPageGroupBox)
self.NextPageSpinBox.setGeometry(QtCore.QRect(10, 20, 81, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.NextPageSpinBox.setFont(font)
self.NextPageSpinBox.setFocusPolicy(QtCore.Qt.StrongFocus)
self.NextPageSpinBox.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
self.NextPageSpinBox.setButtonSymbols(QtWidgets.QAbstractSpinBox.PlusMinus)
self.NextPageSpinBox.setAccelerated(True)
self.NextPageSpinBox.setMaximum(999999999)
self.NextPageSpinBox.setDisplayIntegerBase(10)
self.NextPageSpinBox.setObjectName("NextPageSpinBox")
self.ZindexGroupBox = QtWidgets.QGroupBox(self.centralwidget)
self.ZindexGroupBox.setEnabled(True)
self.ZindexGroupBox.setGeometry(QtCore.QRect(350, 10, 71, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.ZindexGroupBox.setFont(font)
self.ZindexGroupBox.setFocusPolicy(QtCore.Qt.ClickFocus)
self.ZindexGroupBox.setCheckable(True)
self.ZindexGroupBox.setChecked(False)
self.ZindexGroupBox.setObjectName("ZindexGroupBox")
self.ZindexSpinBox = QtWidgets.QSpinBox(self.ZindexGroupBox)
self.ZindexSpinBox.setGeometry(QtCore.QRect(10, 20, 51, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.ZindexSpinBox.setFont(font)
self.ZindexSpinBox.setObjectName("ZindexSpinBox")
self.AddChangeFrame = QtWidgets.QFrame(self.centralwidget)
self.AddChangeFrame.setEnabled(True)
self.AddChangeFrame.setGeometry(QtCore.QRect(420, 0, 351, 71))
font = QtGui.QFont()
font.setPointSize(8)
self.AddChangeFrame.setFont(font)
self.AddChangeFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.AddChangeFrame.setFrameShadow(QtWidgets.QFrame.Raised)
self.AddChangeFrame.setObjectName("AddChangeFrame")
self.NewButtonsSpinBox = QtWidgets.QSpinBox(self.AddChangeFrame)
self.NewButtonsSpinBox.setGeometry(QtCore.QRect(10, 20, 41, 31))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(False)
font.setWeight(50)
self.NewButtonsSpinBox.setFont(font)
self.NewButtonsSpinBox.setMouseTracking(True)
self.NewButtonsSpinBox.setAlignment(QtCore.Qt.AlignCenter)
self.NewButtonsSpinBox.setMinimum(1)
self.NewButtonsSpinBox.setObjectName("NewButtonsSpinBox")
self.NewButtonsRadio = QtWidgets.QRadioButton(self.AddChangeFrame)
self.NewButtonsRadio.setGeometry(QtCore.QRect(0, 0, 161, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.NewButtonsRadio.setFont(font)
self.NewButtonsRadio.setFocusPolicy(QtCore.Qt.ClickFocus)
self.NewButtonsRadio.setObjectName("NewButtonsRadio")
self.StartItemLabel = QtWidgets.QLabel(self.AddChangeFrame)
self.StartItemLabel.setGeometry(QtCore.QRect(70, 20, 61, 21))
self.StartItemLabel.setObjectName("StartItemLabel")
self.EndingItemLabel = QtWidgets.QLabel(self.AddChangeFrame)
self.EndingItemLabel.setGeometry(QtCore.QRect(160, 20, 51, 20))
self.EndingItemLabel.setObjectName("EndingItemLabel")
self.StartItemNumberSpinBox = QtWidgets.QSpinBox(self.AddChangeFrame)
self.StartItemNumberSpinBox.setGeometry(QtCore.QRect(60, 40, 81, 22))
self.StartItemNumberSpinBox.setObjectName("StartItemNumberSpinBox")
self.EndItemNumberSpinBox = QtWidgets.QSpinBox(self.AddChangeFrame)
self.EndItemNumberSpinBox.setGeometry(QtCore.QRect(150, 40, 81, 22))
self.EndItemNumberSpinBox.setObjectName("EndItemNumberSpinBox")
self.ChangeRadioButton = QtWidgets.QRadioButton(self.AddChangeFrame)
self.ChangeRadioButton.setGeometry(QtCore.QRect(170, 0, 181, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.ChangeRadioButton.setFont(font)
self.ChangeRadioButton.setChecked(True)
self.ChangeRadioButton.setObjectName("ChangeRadioButton")
self.SubmitChangesButton = QtWidgets.QPushButton(self.centralwidget)
self.SubmitChangesButton.setGeometry(QtCore.QRect(660, 30, 131, 41))
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(11)
font.setBold(True)
font.setItalic(True)
font.setWeight(75)
font.setKerning(True)
self.SubmitChangesButton.setFont(font)
self.SubmitChangesButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.SubmitChangesButton.setAutoFillBackground(True)
self.SubmitChangesButton.setObjectName("SubmitChangesButton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Simphony Button Editor"))
self.StyleGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Changes the style & color of the button using the drop-down menu.</p></body></html>"))
self.StyleGroupBox.setWhatsThis(_translate("MainWindow", "<html><head/><body><p>Changes style & color of button</p></body></html>"))
self.StyleGroupBox.setTitle(_translate("MainWindow", "Color/ Style"))
self.StyleComboBox.setItemText(0, _translate("MainWindow", "Button1"))
self.StyleComboBox.setItemText(1, _translate("MainWindow", "Button2"))
self.StyleComboBox.setItemText(2, _translate("MainWindow", "Button3"))
self.StyleComboBox.setItemText(3, _translate("MainWindow", "Button4"))
self.StyleComboBox.setItemText(4, _translate("MainWindow", "Button5"))
self.FontGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Changes the font size the button text.</p></body></html>"))
self.FontGroupBox.setTitle(_translate("MainWindow", "Font Size"))
self.OpacityGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Changes the percentage Opacity (visibility) of the button.</p></body></html>"))
self.OpacityGroupBox.setTitle(_translate("MainWindow", "Opacity"))
self.NextPageGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Assigns the next menu page to jump to after button completion. </p></body></html>"))
self.NextPageGroupBox.setTitle(_translate("MainWindow", "Next Page"))
self.ZindexGroupBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Changes the visibility layer of the button. If a button is set to 1 and another set to 2 in the same spot then you would only see the button with Z index 2.</p></body></html>"))
self.ZindexGroupBox.setTitle(_translate("MainWindow", "Z Index"))
self.NewButtonsRadio.setText(_translate("MainWindow", "Create New Buttons"))
self.StartItemLabel.setText(_translate("MainWindow", "Start Item#"))
self.EndingItemLabel.setText(_translate("MainWindow", "End Item#"))
self.StartItemNumberSpinBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Enter Item number. Items must be in sequintial order if adding multiple buttons.</p></body></html>"))
self.EndItemNumberSpinBox.setToolTip(_translate("MainWindow", "<html><head/><body><p>Items must be in sequintial order if adding multiple buttons.</p></body></html>"))
self.ChangeRadioButton.setText(_translate("MainWindow", "Update Copied Buttons"))
self.SubmitChangesButton.setText(_translate("MainWindow", "Make Changes"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.SubmitChangesButton.clicked.connect(self.on_clicked)
# + vvvv
self.itemTextComboBox = "Button1"
self.StyleComboBox.activated[str].connect(self.activatedItemComboBox)
def activatedItemComboBox(self, text):
self.itemTextComboBox = text
# + ^^^^
#QtCore.pyqtSlot()
def on_clicked(self):
if self.StyleGroupBox.isChecked() is True:
print(self.itemTextComboBox)
if self.FontGroupBox.isChecked() is True:
print('Font Size = {}'.format(self.FontSpinBox.value()))
if self.OpacityGroupBox.isChecked() is True:
print('Opacity% = {:.2f}'.format(self.OpacityDoubleSpinBox.value()))
if self.NextPageGroupBox.isChecked() is True:
print('Next Page = {}'.format(self.NextPageSpinBox.value()))
if self.ZindexGroupBox.isChecked() is True:
print('Z Index = {}'.format(self.ZindexSpinBox.value()))
if __name__ == "__main__":
import sys
# import MainCode
# import MyDesign
app = QtWidgets.QApplication(sys.argv)
# MainWindow = QtWidgets.QMainWindow()
# ui = Ui_MainWindow()
# ui.setupUi(MainWindow)
# MainWindow.show()
# print(Ui_MainWindow)
w = MainWindow() # <---
w.show() # <---
sys.exit(app.exec_())

Categories