Pyqt5 Stop Function with Stop Button - python

We control our process with variables like "self.runHunt". If we get the "True" value, we start the time and print our text. When we press stop, we tell it to take the value "false" and stop the process. I think there is no problem here.
But;
Time starts when our function is "while true". At that time, even if we press the stop button, time continues and our value still remains as "True" and cannot update itself. However, after the time expires, the value becomes "false".
My question is how can I get the "false" variable without waiting for the time to complete?
from PyQt5 import QtCore, QtGui, QtWidgets
from operator import index
from PyQt5 import QtCore, QtGui, QtWidgets, QtTest
import random
import threading
import sys
myKeyboard = Controller()
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(850, 350)
Dialog.setMinimumSize(QtCore.QSize(850, 350))
Dialog.setMaximumSize(QtCore.QSize(850, 350))
self.stopPushButton = QtWidgets.QPushButton(Dialog)
self.stopPushButton.setGeometry(QtCore.QRect(580, 90, 241, 51))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.stopPushButton.setFont(font)
self.stopPushButton.setIconSize(QtCore.QSize(32, 32))
self.stopPushButton.setObjectName("stopPushButton")
self.startPushButton = QtWidgets.QPushButton(Dialog)
self.startPushButton.setGeometry(QtCore.QRect(580, 20, 241, 51))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.startPushButton.setFont(font)
self.startPushButton.setIconSize(QtCore.QSize(32, 32))
self.startPushButton.setObjectName("startPushButton")
self.loopGroup = QtWidgets.QGroupBox(Dialog)
self.loopGroup.setEnabled(True)
self.loopGroup.setGeometry(QtCore.QRect(10, 10, 541, 221))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.loopGroup.setFont(font)
self.loopGroup.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
self.loopGroup.setAcceptDrops(False)
self.loopGroup.setAutoFillBackground(False)
self.loopGroup.setFlat(False)
self.loopGroup.setCheckable(False)
self.loopGroup.setObjectName("loopGroup")
self.huntCheckBox = QtWidgets.QCheckBox(self.loopGroup)
self.huntCheckBox.setGeometry(QtCore.QRect(240, 35, 35, 20))
self.huntCheckBox.setText("")
self.huntCheckBox.setObjectName("huntCheckBox")
self.huntLabel = QtWidgets.QLabel(self.loopGroup)
self.huntLabel.setGeometry(QtCore.QRect(20, 35, 61, 21))
font = QtGui.QFont()
font.setFamily("Microsoft JhengHei UI")
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.huntLabel.setFont(font)
self.huntLabel.setObjectName("huntLabel")
self.HuntinfoBrowser = QtWidgets.QTextBrowser(self.loopGroup)
self.HuntinfoBrowser.setGeometry(QtCore.QRect(280, 25, 251, 31))
self.HuntinfoBrowser.setObjectName("HuntinfoBrowser")
self.huntCmbBox = QtWidgets.QComboBox(self.loopGroup)
self.huntCmbBox.setGeometry(QtCore.QRect(100, 25, 121, 31))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
self.huntCmbBox.setFont(font)
self.huntCmbBox.setMouseTracking(False)
self.huntCmbBox.setEditable(False)
self.huntCmbBox.setMaxCount(100)
self.huntCmbBox.setInsertPolicy(QtWidgets.QComboBox.InsertAtBottom)
self.huntCmbBox.setObjectName("huntCmbBox")
self.huntCmbBox.addItem("")
self.HuntinfoLabel = QtWidgets.QLabel(self.loopGroup)
self.HuntinfoLabel.setGeometry(QtCore.QRect(290, 30, 231, 21))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
self.HuntinfoLabel.setFont(font)
self.HuntinfoLabel.setText("")
self.HuntinfoLabel.setObjectName("HuntinfoLabel")
self.retranslateUi(Dialog)
self.huntCmbBox.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(Dialog)
hnt1 = " 10 - 15 sec. "
hnt2 = " 16 - 20 sec. "
#HUNT
self.huntCmbBox.addItem(hnt1,[10.1, 15.1])
self.huntCmbBox.addItem(hnt2,[16.1, 20.1])
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.stopPushButton.setText(_translate("Dialog", "STOP ALL"))
self.startPushButton.setText(_translate("Dialog", "START"))
self.huntLabel.setText(_translate("Dialog", "Hunt"))
self.huntCmbBox.setCurrentText(_translate("Dialog", " Select Time"))
self.huntCmbBox.setItemText(0, _translate("Dialog", " Select Time"))
self.runHunt = False
self.stop = False
self.startPushButton.clicked.connect(self.start)
self.stopPushButton.clicked.connect(self.stopFunc)
self.stopPushButton.setEnabled(False)
def stopFunc(self):
self.runHunt= False
self.stopPushButton.setEnabled(False)
self.startPushButton.setEnabled(True)
def huntFunction(self):
index1 = self.huntCmbBox.currentIndex()
self.seciliCmBox = self.huntCmbBox.currentData()
self.HuntinfoLabel.clear()
if index1 == 0 and self.runHunt:
self.HuntinfoLabel.clear()
self.HuntinfoLabel.setText(self.HuntinfoLabel.text() + " Error. Stop and select time!")
else:
self.HuntinfoLabel.clear()
self.HuntinfoLabel.setText(self.HuntinfoLabel.text() + " On The Way!")
while True:
if self.runHunt:
randomTimer = float(random.uniform(self.seciliCmBox[0],self.seciliCmBox[1]))
QtTest.QTest.qWait(randomTimer * 1000)
self.HuntinfoLabel.setText("in time runhunt is: "+ str(self.runHunt))
if self.runHunt:
#bla bla code
self.HuntinfoLabel.clear()
self.HuntinfoLabel.setText("in text runhunt is: "+ str(self.runHunt))
def start(self):
self.startPushButton.setEnabled(False)
self.stopPushButton.setEnabled(True)
t1 = threading.Thread(target=self.huntFunction)
if self.huntCheckBox.isChecked():
self.runHunt = True
t1.start()
else:
self.HuntinfoLabel.clear()
self.HuntinfoLabel.setText(self.HuntinfoLabel.text() + " You need check hunt box.")
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_())

Related

PyQt5 QPushbutton to store ariables 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)

deleting a text from QtextEdit after pressing a pushbutton Pyqt5

this a chatbot using chatterbot i m trying to delete the text that the user writes in textEdit once he clicks a pushbutton or presses Enter like tkinter you can do like: msgs.insert(END, "You : " + query) and it deletes the text once the button is pressed
from PyQt5 import QtCore, QtGui, QtWidgets
from chatterbot import ChatBot
bot = ChatBot('butty bot')
trainer = ChatterBotCorpusTrainer(bot)
trainer.train("chatterbot.corpus.english")
bot = ChatBot('butty bot')
trainer = ChatterBotCorpusTrainer(bot)
trainer.train("chatterbot.corpus.english")
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(402, 659)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
def action():
query = self.textEdit.toPlainText()
response = bot.get_response(query)
self.listbox.addItem("You : " + query)
self.listbox.addItem("Butty bot : " + str(response))
self.send_btn = QtWidgets.QPushButton(self.centralwidget)
self.send_btn.setGeometry(QtCore.QRect(140, 550, 111, 41))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.send_btn.setFont(font)
self.send_btn.setObjectName("send_btn")
self.send_btn.clicked.connect(action)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(140, 10, 121, 21))
font = QtGui.QFont()
font.setFamily("Lucida Fax")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(10, 510, 381, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.textEdit.setFont(font)
self.textEdit.setObjectName("textEdit")

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_())

How to show the live preview from picamera in a GUI Window made by PyQt5?

I am designing a GUI to capture an image from picamera, I am using PyQt5 to develop the GUI. I used camera.start_preview() function to see the live preview. After that I used a button named "Take" to capture the image and after capturing the image I showed the image in Qlabel by using self.label_2.setPixmap(QtGui.QPixmap()) command till this my code is working fine. Now I want to set the live preview of PiCamera into the same Qlabel, which I am using to show the captured image. Is it possible to set the live preview inside a QLabel?
After googling I found PIL can be used to stream live preview inside an window, but I have no idea how to implement it.
from PyQt5 import QtCore, QtGui, QtWidgets
from picamera import PiCamera
import time
from time import sleep
import gpiozero
from gpiozero import Button
from PIL import Image
import cups
import os
camera = PiCamera()
camera.sensor_mode = 2
camera.brightness = 70
camera.contrast = 20
photoHeight = 1944
photoWidth = 2592
class clsPicCount(object):
countpic=0
def __init__(self):
clsPicCount.countpic=clsPicCount.countpic+1
if clsPicCount.countpic > 1:
clsPicCount.countpic = 0
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1366, 768)
MainWindow.setMinimumSize(QtCore.QSize(1366, 768))
MainWindow.setMaximumSize(QtCore.QSize(1366, 768))
font = QtGui.QFont()
font.setUnderline(False)
font.setStrikeOut(False)
MainWindow.setFont(font)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("Photo.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.retake = QtWidgets.QPushButton(self.centralwidget)
self.retake.setGeometry(QtCore.QRect(320, 680, 211, 71))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.retake.setFont(font)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("retake.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
self.retake.setIcon(icon1)
self.retake.setIconSize(QtCore.QSize(50, 50))
self.retake.setObjectName("retake")
self.take = QtWidgets.QPushButton(self.centralwidget)
self.take.setGeometry(QtCore.QRect(550, 680, 211, 71))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.take.setFont(font)
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("take.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
self.take.setIcon(icon2)
self.take.setIconSize(QtCore.QSize(60, 60))
self.take.setObjectName("take")
self.print = QtWidgets.QPushButton(self.centralwidget)
self.print.setGeometry(QtCore.QRect(780, 680, 211, 71))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.print.setFont(font)
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap("print.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
self.print.setIcon(icon3)
self.print.setIconSize(QtCore.QSize(50, 50))
self.print.setCheckable(True)
self.print.setObjectName("print")
self.settings = QtWidgets.QPushButton(self.centralwidget)
self.settings.setGeometry(QtCore.QRect(1220, 690, 121, 51))
font = QtGui.QFont()
font.setPointSize(8)
font.setBold(True)
font.setWeight(75)
self.settings.setFont(font)
icon4 = QtGui.QIcon()
icon4.addPixmap(QtGui.QPixmap("settings.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
self.settings.setIcon(icon4)
self.settings.setIconSize(QtCore.QSize(30, 30))
self.settings.setObjectName("settings")
self.heading = QtWidgets.QLabel(self.centralwidget)
self.heading.setGeometry(QtCore.QRect(390, 20, 581, 31))
self.heading.setObjectName("heading")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(230, 70, 900, 600))
self.label_2.setText("")
self.label_2.setPixmap(QtGui.QPixmap("photobooth.jpg"))
self.label_2.setScaledContents(True)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(0, 0, 1361, 771))
self.label_3.setText("")
self.label_3.setPixmap(QtGui.QPixmap("background.png"))
self.label_3.setObjectName("label_3")
self.label_3.raise_()
self.retake.raise_()
self.take.raise_()
self.print.raise_()
self.settings.raise_()
self.heading.raise_()
self.label_2.raise_()
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#Defining functions of our buttons
self.take.clicked.connect(self.startPreview)
#Initializing Printer
conn = cups.Connection ()
printers = conn.getPrinters ()
printer_name = list(printers)[0]
for printer in printers:
print (printer, printers[printer]["device-uri"])
#Code Block for live preview and image capture
def startPreview(self):
p=clsPicCount()
print(clsPicCount.countpic)
if clsPicCount.countpic == 1:
camera.start_preview(fullscreen=False, window=(10,0,320,240))
else:
filename = time.strftime("%Y%m%d-%H%M%S")
print (filename)
camera.capture('/home/pi/Desktop/%s.jpg'%filename, resize = (photoWidth, photoHeight))
camera.stop_preview()
self.label_2.setPixmap(QtGui.QPixmap('/home/pi/Desktop/%s.jpg'%filename))
self.label_2.setScaledContents(True)
#self.label_3.raise_()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "PhotoBooth"))
self.retake.setText(_translate("MainWindow", "RETAKE"))
self.take.setText(_translate("MainWindow", "TAKE"))
self.print.setText(_translate("MainWindow", "PRINT"))
self.settings.setText(_translate("MainWindow", "SETTINGS"))
self.heading.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:14pt; font-weight:600; color:#ffffff;\">TECH MECH SERVICES PVT LTD.</span></p></body></html>"))
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_())

Getting an error while trying to close and open current dialog window in python

SignIn.py
from PyQt5 import QtCore, QtGui, QtWidgets
from HomePage import Ui_HomePage
from PyQt5.QtWidgets import QMessageBox
import cx_Oracle
class Ui_SignIn(object):
def openConnection(self):
con=cx_Oracle.connect('lab/oracle#localhost:1521/xe')
def showMessageBox(self,title,message):
#QMessageBox.about(self, "Title", "Message")
msgBox = QtWidgets.QMessageBox()
msgBox.setIcon(QtWidgets.QMessageBox.Warning)
msgBox.setWindowTitle(title)
msgBox.setText(message)
msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok)
msgBox.exec_()
def loginCheck(self):
username = self.ledt_username.text()
password = self.ledt_password.text()
#print(username)
#print(password)
con=cx_Oracle.connect('lab/oracle#localhost:1521/xe')
cur=con.cursor()
cur.execute('select * from user_tbl where username=:1 and password=:2', (username, password))
#cur.execute('select * from user_tbl')
cur.fetchall()
#n=0
#for result in cur:
#n=n+1
#m=cur.rowcount
#print(n)
#print(m)
if(cur.rowcount > 0):
print("User Found ! ")
Dialog.hide()
self.welcomeWindowShow()
else:
print("User Not Found !")
#self.showMessageBox()
self.showMessageBox('Warning','Invalid Username And Password')
#self.welcomeWindowShow()
con.close()
def welcomeWindowShow(self):
self.window=QtWidgets.QDialog()
self.ui=Ui_HomePage()
self.ui.setupUi(self.window)
self.window.show()
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(690, 450)
Dialog.setBaseSize(QtCore.QSize(0, 0))
self.lbl_username = QtWidgets.QLabel(Dialog)
self.lbl_username.setGeometry(QtCore.QRect(250, 100, 61, 21))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.lbl_username.setFont(font)
self.lbl_username.setLayoutDirection(QtCore.Qt.RightToLeft)
self.lbl_username.setAutoFillBackground(False)
self.lbl_username.setObjectName("lbl_username")
self.ledt_username = QtWidgets.QLineEdit(Dialog)
self.ledt_username.setGeometry(QtCore.QRect(330, 100, 141, 21))
self.ledt_username.setObjectName("ledt_username")
self.lbl_password = QtWidgets.QLabel(Dialog)
self.lbl_password.setGeometry(QtCore.QRect(250, 150, 71, 21))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.lbl_password.setFont(font)
self.lbl_password.setAutoFillBackground(False)
self.lbl_password.setObjectName("lbl_password")
self.ledt_password = QtWidgets.QLineEdit(Dialog)
self.ledt_password.setGeometry(QtCore.QRect(330, 150, 141, 20))
self.ledt_password.setObjectName("ledt_password")
self.pbtn_signin = QtWidgets.QPushButton(Dialog)
self.pbtn_signin.setGeometry(QtCore.QRect(270, 200, 75, 23))
self.pbtn_signin.setObjectName("pbtn_signin")
############## logincheck calling ######
self.pbtn_signin.clicked.connect(self.loginCheck)
###################################################
self.pbtn_signup = QtWidgets.QPushButton(Dialog)
self.pbtn_signup.setGeometry(QtCore.QRect(380, 200, 75, 23))
self.pbtn_signup.setObjectName("pbtn_signup")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.lbl_username.setText(_translate("Dialog", "Username"))
self.lbl_password.setText(_translate("Dialog", "Password"))
self.pbtn_signin.setText(_translate("Dialog", "Sign In"))
self.pbtn_signup.setText(_translate("Dialog", "Sign Up"))
# from HomePage import Ui_HomePage
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_SignIn()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
HomePage.py
from PyQt5 import QtCore, QtGui, QtWidgets
#from SignIn import Ui_SignIn
class Ui_HomePage(object):
def importSignIn(self):
from SignIn import Ui_SignIn
def signInWindowShow(self):
Dialog.hide()
from SignIn import Ui_SignIn
self.window=QtWidgets.QDialog()
self.ui=Ui_SignIn()
self.ui.setupUi(self.window)
self.window.show()
#self.Dialog.hide()
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(696, 395)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(210, 50, 161, 41))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(300, 220, 75, 23))
self.pushButton.setObjectName("pushButton")
############## logincheck calling ######
self.pushButton.clicked.connect(self.signInWindowShow)
###################################################
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", "Welcome"))
self.pushButton.setText(_translate("Dialog", "Logout"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_HomePage()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
Getting an error while trying to close and open current dialog window in python
I need to open and close two windows in loop.
also attaching the error that i got.
enter image description here
The reason of the error is that Dialog is not available in scope of loginCheck method and need to be passed explicitly as a param in the following line:
self.pbtn_signin.clicked.connect(self.loginCheck)
Here is the question with the answers, how to do it.
One of the options would be import partial from functools:
from functools import partial
And use this function to pass Dialog to loginCheck:
self.pbtn_signin.clicked.connect(partial(self.loginCheck, Dialog))

Categories