PySide open a new window, but close immediately - python

I have a question. Although some questions are similar with mine, but i cat't find how to resolve.
I create a login window, when click 'Confirm' button, hope to load a new window. But now the new window will show and close immediately.The code is below:
# -*- coding: utf-8 -*-
import sys
import time
from PySide.QtCore import *
from PySide.QtGui import *
import login
import moreFrame_modify
class show_login(QWidget,login.Ui_Form):
def __init__(self,parent=None):
super(show_login,self).__init__(parent)
self.ui = login.Ui_Form()
self.ui.setupUi(self)
def show_notepad_clk(self): #This function will trigger a new window
print "Hello world!"
# farm.destroy()
time.sleep(1)
showMemoUI().show()
def exit_login_show(self):
time.sleep(.1)
exit()
# self.destroy()
def create_new_user(self):
print "Create"
QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
class showMemoUI(QWidget,moreFrame_modify.Ui_Form):
def __init__(self,parent=None):
super(showMemoUI,self).__init__(parent)
self.ui = moreFrame_modify.Ui_Form()
print "Start show"
self.ui.setupUi(self)
print "Show notepad UI"
if __name__=='__main__':
app = QApplication(sys.argv)
farm = show_login()
farm.show()
# farm = showMemoUI()
# farm.show()
sys.exit(app.exec_())
login.py
# -- coding: utf-8 --
# Form implementation generated from reading ui file 'login.ui'
#
# Created: Mon Nov 12 17:25:34 2018
# by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost!
from PySide import QtCore, QtGui
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(291, 207)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/a15.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Form.setWindowIcon(icon)
self.verticalLayout_3 = QtGui.QVBoxLayout(Form)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.verticalLayout_2 = QtGui.QVBoxLayout()
self.verticalLayout_2.setSpacing(20)
self.verticalLayout_2.setContentsMargins(20, -1, 20, 20)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtGui.QLabel(Form)
font = QtGui.QFont()
font.setPointSize(10)
self.label.setFont(font)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.userTxt = QtGui.QLineEdit(Form)
self.userTxt.setObjectName("userTxt")
self.horizontalLayout.addWidget(self.userTxt)
self.verticalLayout_2.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtGui.QLabel(Form)
font = QtGui.QFont()
font.setPointSize(10)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.pwdTxt = QtGui.QLineEdit(Form)
self.pwdTxt.setObjectName("pwdTxt")
self.horizontalLayout_2.addWidget(self.pwdTxt)
self.verticalLayout_2.addLayout(self.horizontalLayout_2)
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setSpacing(10)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_3 = QtGui.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.confirmBtn = QtGui.QPushButton(Form)
font = QtGui.QFont()
font.setPointSize(10)
self.confirmBtn.setFont(font)
self.confirmBtn.setObjectName("confirmBtn")
self.horizontalLayout_3.addWidget(self.confirmBtn)
self.cancelBtn = QtGui.QPushButton(Form)
font = QtGui.QFont()
font.setPointSize(10)
self.cancelBtn.setFont(font)
self.cancelBtn.setObjectName("cancelBtn")
self.horizontalLayout_3.addWidget(self.cancelBtn)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.regeisterBtn = QtGui.QPushButton(Form)
font = QtGui.QFont()
font.setPointSize(10)
self.regeisterBtn.setFont(font)
self.regeisterBtn.setObjectName("regeisterBtn")
self.verticalLayout.addWidget(self.regeisterBtn)
self.verticalLayout_2.addLayout(self.verticalLayout)
self.verticalLayout_3.addLayout(self.verticalLayout_2)
self.retranslateUi(Form)
QtCore.QObject.connect(self.confirmBtn, QtCore.SIGNAL("clicked()"), Form.show_notepad_clk)
QtCore.QObject.connect(self.cancelBtn, QtCore.SIGNAL("clicked()"), Form.exit_login_show)
QtCore.QObject.connect(self.regeisterBtn, QtCore.SIGNAL("clicked()"), Form.create_new_user)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Login", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Form", "UserName", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("Form", "Password", None, QtGui.QApplication.UnicodeUTF8))
self.confirmBtn.setText(QtGui.QApplication.translate("Form", "Confirm", None, QtGui.QApplication.UnicodeUTF8))
self.cancelBtn.setText(QtGui.QApplication.translate("Form", "Cancer", None, QtGui.QApplication.UnicodeUTF8))
self.regeisterBtn.setText(QtGui.QApplication.translate("Form", "Add a new user", None, QtGui.QApplication.UnicodeUTF8))
import myresource_rc
moreFrame_modify.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'moreFrame_modify.ui'
#
# Created: Mon Nov 12 17:25:38 2018
# by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost!
from PySide import QtCore, QtGui
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(469, 458)
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setWeight(75)
font.setBold(True)
Form.setFont(font)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/a11.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Form.setWindowIcon(icon)
self.horizontalLayout_10 = QtGui.QHBoxLayout(Form)
self.horizontalLayout_10.setObjectName("horizontalLayout_10")
self.horizontalLayout_9 = QtGui.QHBoxLayout()
self.horizontalLayout_9.setObjectName("horizontalLayout_9")
self.frame_3 = QtGui.QFrame(Form)
self.frame_3.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtGui.QFrame.Raised)
self.frame_3.setObjectName("frame_3")
self.horizontalLayout_4 = QtGui.QHBoxLayout(self.frame_3)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_2 = QtGui.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label = QtGui.QLabel(self.frame_3)
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(12)
font.setWeight(75)
font.setBold(True)
self.label.setFont(font)
self.label.setObjectName("label")
self.horizontalLayout_2.addWidget(self.label)
self.dateEdit = QtGui.QDateEdit(self.frame_3)
self.dateEdit.setObjectName("dateEdit")
self.horizontalLayout_2.addWidget(self.dateEdit)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtGui.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_2 = QtGui.QLabel(self.frame_3)
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(12)
font.setWeight(75)
font.setBold(True)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.horizontalLayout_3.addWidget(self.label_2)
self.timeEdit = QtGui.QTimeEdit(self.frame_3)
self.timeEdit.setObjectName("timeEdit")
self.horizontalLayout_3.addWidget(self.timeEdit)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_3 = QtGui.QLabel(self.frame_3)
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(12)
font.setWeight(75)
font.setBold(True)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.horizontalLayout.addWidget(self.label_3)
self.addrTxt = QtGui.QLineEdit(self.frame_3)
self.addrTxt.setObjectName("addrTxt")
self.horizontalLayout.addWidget(self.addrTxt)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_5 = QtGui.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.label_4 = QtGui.QLabel(self.frame_3)
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(12)
font.setWeight(75)
font.setBold(True)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.horizontalLayout_5.addWidget(self.label_4)
self.commTxt = QtGui.QLineEdit(self.frame_3)
self.commTxt.setObjectName("commTxt")
self.horizontalLayout_5.addWidget(self.commTxt)
self.verticalLayout.addLayout(self.horizontalLayout_5)
self.textBrowser = QtGui.QTextBrowser(self.frame_3)
self.textBrowser.setObjectName("textBrowser")
self.verticalLayout.addWidget(self.textBrowser)
self.horizontalLayout_4.addLayout(self.verticalLayout)
self.verticalLayout_2 = QtGui.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.pushButton_4 = QtGui.QPushButton(self.frame_3)
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(12)
font.setWeight(75)
font.setBold(True)
self.pushButton_4.setFont(font)
self.pushButton_4.setObjectName("pushButton_4")
self.verticalLayout_2.addWidget(self.pushButton_4)
self.pushButton_5 = QtGui.QPushButton(self.frame_3)
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(12)
font.setWeight(75)
font.setBold(True)
self.pushButton_5.setFont(font)
self.pushButton_5.setObjectName("pushButton_5")
self.verticalLayout_2.addWidget(self.pushButton_5)
self.pushButton_6 = QtGui.QPushButton(self.frame_3)
font = QtGui.QFont()
font.setFamily("Yu Gothic UI Semibold")
font.setPointSize(12)
font.setWeight(75)
font.setBold(True)
self.pushButton_6.setFont(font)
self.pushButton_6.setObjectName("pushButton_6")
self.verticalLayout_2.addWidget(self.pushButton_6)
spacerItem = QtGui.QSpacerItem(20, 293, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.verticalLayout_2.addItem(spacerItem)
self.horizontalLayout_4.addLayout(self.verticalLayout_2)
self.horizontalLayout_9.addWidget(self.frame_3)
self.horizontalLayout_10.addLayout(self.horizontalLayout_9)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Notepad", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Form", "Date", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("Form", "Time", None, QtGui.QApplication.UnicodeUTF8))
self.label_3.setText(QtGui.QApplication.translate("Form", "Address", None, QtGui.QApplication.UnicodeUTF8))
self.label_4.setText(QtGui.QApplication.translate("Form", "Comment", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_4.setText(QtGui.QApplication.translate("Form", "Save", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_5.setText(QtGui.QApplication.translate("Form", "Cancel", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton_6.setText(QtGui.QApplication.translate("Form", "About", None, QtGui.QApplication.UnicodeUTF8))
import myresource_rc
Add login.py and moreFrame_Modify.py ,these two files are generated by QT designer.

In your case you have 2 errors:
You should not use time.sleep() in a GUI, in the case of Qt you should use QTimer since time.sleep() freezes the GUI.
A local variable created in a function only exists as long as the function is executed, in your case showMemoUI is shown and instantly eliminated, therefore the new window is not observed. The solution is to make the new window member of the class.
Using the above, the solution code is:
import sys
from PySide import QtCore, QtGui
import login
import moreFrame_modify
class show_login(QtGui.QWidget, login.Ui_Form):
def __init__(self,parent=None):
super(show_login,self).__init__(parent)
self.setupUi(self)
self.showmemoui = showMemoUI()
def show_notepad_clk(self): #This function will trigger a new window
print("Hello world!")
QtCore.QTimer.singleShot(1000, self.showmemoui.show)
def exit_login_show(self):
QtCore.QTimer.singleShot(100, QtGui.QApplication.quit)
def create_new_user(self):
print("Create")
QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
class showMemoUI(QtGui.QWidget, moreFrame_modify.Ui_Form):
def __init__(self, parent=None):
super(showMemoUI,self).__init__(parent)
print("Start show")
self.setupUi(self)
print("Show notepad UI")
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
farm = show_login()
farm.show()
sys.exit(app.exec_())

Related

How to pass parameters bw two windows in pyqt5?

I am working on a password manager gui app. Using Qtdesigner I created a main window where I asked for username and password from user. and a dialog window where display the user their username and passwords for different apps and websites. I also called that dialog window from my main window so that when user press login button then if their login credential is correct , the dialog window will appear.
Now what I want is that when dialog window appears,after clicking push button it should show all data of the user which is stored in a table with that username(like if username is alex then a table with name alex is there in db with username and passwords of different apps/websites).
Hope my question is understandable enough to be answered. I can further clarify if anyone interested to answer.
MY mainwindow file------>
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
from main2 import Ui_Dialog
class Ui_MainWindow(object):
def openwindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_Dialog()
self.ui.setupUi(self.window)
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(553, 400)
MainWindow.setMinimumSize(QtCore.QSize(553, 400))
MainWindow.setMaximumSize(QtCore.QSize(553, 400))
MainWindow.setAutoFillBackground(False)
MainWindow.setStyleSheet("background-color:\"PowderBlue\"")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(-1, -5, 561, 61))
font = QtGui.QFont()
font.setFamily("Dosis ExtraBold")
font.setPointSize(30)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setStyleSheet("color: rgb(87, 8, 97);")
self.label.setFrameShape(QtWidgets.QFrame.NoFrame)
self.label.setFrameShadow(QtWidgets.QFrame.Plain)
self.label.setLineWidth(7)
self.label.setObjectName("label")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(28, 120, 511, 261))
self.frame.setStyleSheet("background-color: rgb(176, 224, 230);\n"
"")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.username = QtWidgets.QLineEdit(self.frame)
self.username.setGeometry(QtCore.QRect(224, 0, 261, 41))
font = QtGui.QFont()
font.setFamily("Segoe Print")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
font.setStyleStrategy(QtGui.QFont.PreferAntialias)
self.username.setFont(font)
self.username.setStyleSheet("border :2px solid purple ;\n"
"background-color:PowderBlue;\n"
"border-radius:5px;\n"
"\n"
"\n"
"\n"
"")
self.username.setText("")
self.username.setEchoMode(QtWidgets.QLineEdit.Normal)
self.username.setPlaceholderText("master user")
self.username.setClearButtonEnabled(False)
self.username.setObjectName("username")
self.label_2 = QtWidgets.QLabel(self.frame)
self.label_2.setGeometry(QtCore.QRect(442, 7, 41, 31))
self.label_2.setStyleSheet("image: url(:/user.png);\n"
"background-color:PowderBlue")
self.label_2.setText("")
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.frame)
self.label_3.setGeometry(QtCore.QRect(450, 69, 31, 31))
self.label_3.setAutoFillBackground(False)
self.label_3.setStyleSheet("image: url(:/key.png);\n"
"background-color:PowderBlue")
self.label_3.setText("")
self.label_3.setObjectName("label_3")
self.password = QtWidgets.QLineEdit(self.frame)
self.password.setGeometry(QtCore.QRect(225, 65, 261, 41))
font = QtGui.QFont()
font.setFamily("Segoe Print")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
font.setStyleStrategy(QtGui.QFont.PreferAntialias)
self.password.setFont(font)
self.password.setPlaceholderText("master password")
self.password.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
self.password.setStyleSheet("border :2px solid purple ;\n"
"border-radius:5px;")
self.password.setText("")
self.password.setEchoMode(QtWidgets.QLineEdit.Password)
self.password.setPlaceholderText("")
self.password.setClearButtonEnabled(True)
self.password.setObjectName("password")
self.login_button = QtWidgets.QPushButton(self.frame,clicked=lambda:self.log_in())
self.login_button.setGeometry(QtCore.QRect(280, 130, 41, 31))
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.login_button.setFont(font)
self.login_button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.login_button.setAcceptDrops(True)
self.login_button.setAutoFillBackground(False)
self.login_button.setStyleSheet("image: url(:/login.png);")
self.login_button.setText("")
self.login_button.setFlat(True)
self.login_button.setObjectName("login_button")
self.signup_button = QtWidgets.QPushButton(self.frame,clicked=lambda:self.sign_up())
self.signup_button.setGeometry(QtCore.QRect(388, 130, 41, 31))
self.signup_button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.signup_button.setMouseTracking(False)
self.signup_button.setAcceptDrops(True)
self.signup_button.setAutoFillBackground(False)
self.signup_button.setStyleSheet("image: url(:/user (2).png);")
self.signup_button.setText("")
self.signup_button.setFlat(True)
self.signup_button.setObjectName("signup_button")
self.label_4 = QtWidgets.QLabel(self.frame)
self.label_4.setGeometry(QtCore.QRect(25, 7, 141, 141))
self.label_4.setStyleSheet("image: url(:/team.png);")
self.label_4.setText("")
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.frame)
self.label_5.setGeometry(QtCore.QRect(17, 135, 171, 41))
font = QtGui.QFont()
font.setFamily("Forte")
font.setPointSize(22)
font.setBold(False)
font.setWeight(50)
self.label_5.setFont(font)
self.label_5.setStyleSheet("color: rgb(5, 22, 127);")
self.label_5.setFrameShape(QtWidgets.QFrame.NoFrame)
self.label_5.setFrameShadow(QtWidgets.QFrame.Plain)
self.label_5.setLineWidth(7)
self.label_5.setObjectName("label_5")
self.error_lbl = QtWidgets.QLabel(self.frame)
self.error_lbl.setGeometry(QtCore.QRect(230,165,251,50))
#self.error_lbl.textFormat.RichText()
font = QtGui.QFont()
font.setFamily("Roboto Black")
font.setPointSize(11)
font.setBold(True)
font.setWeight(65)
self.error_lbl.setFont(font)
self.error_lbl.setStyleSheet("color: rgb(255,0,0);")
self.error_lbl.setFrameShape(QtWidgets.QFrame.NoFrame)
self.error_lbl.setFrameShadow(QtWidgets.QFrame.Plain)
self.error_lbl.setLineWidth(1)
self.error_lbl.setObjectName("error_lbl")
self.line_2 = QtWidgets.QFrame(self.frame)
self.line_2.setGeometry(QtCore.QRect(-17, 190, 20, 61))
self.line_2.setFrameShape(QtWidgets.QFrame.VLine)
self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_2.setObjectName("line_2")
self.line_5 = QtWidgets.QFrame(self.frame)
self.line_5.setGeometry(QtCore.QRect(520, -20, 20, 221))
self.line_5.setFrameShape(QtWidgets.QFrame.VLine)
self.line_5.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_5.setObjectName("line_5")
self.label_6 = QtWidgets.QLabel(self.frame)
self.label_6.setGeometry(QtCore.QRect(349, 231, 41, 31))
self.label_6.setStyleSheet("image: url(:/copyright.png);")
self.label_6.setText("")
self.label_6.setObjectName("label_6")
self.label_7 = QtWidgets.QLabel(self.frame)
self.label_7.setGeometry(QtCore.QRect(380, 234, 111, 20))
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_7.setFont(font)
self.label_7.setObjectName("label_7")
self.password.raise_()
self.username.raise_()
self.label_2.raise_()
self.label_3.raise_()
self.login_button.raise_()
self.signup_button.raise_()
self.label_4.raise_()
self.label_5.raise_()
self.error_lbl.raise_()
self.line_2.raise_()
self.line_5.raise_()
self.label_6.raise_()
self.label_7.raise_()
self.info_button = QtWidgets.QPushButton(self.centralwidget)
self.info_button.setGeometry(QtCore.QRect(510, 0, 41, 23))
self.info_button.setStyleSheet("image: url(:/information-button.png);")
self.info_button.setText("")
self.info_button.setFlat(True)
self.info_button.setObjectName("info_button")
self.line_3 = QtWidgets.QFrame(self.centralwidget)
self.line_3.setGeometry(QtCore.QRect(9, 101, 20, 271))
self.line_3.setFrameShape(QtWidgets.QFrame.VLine)
self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_3.setObjectName("line_3")
self.line_6 = QtWidgets.QFrame(self.centralwidget)
self.line_6.setGeometry(QtCore.QRect(520, 101, 21, 271))
self.line_6.setFrameShape(QtWidgets.QFrame.VLine)
self.line_6.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_6.setObjectName("line_6")
self.label_8 = QtWidgets.QLabel(self.centralwidget)
self.label_8.setGeometry(QtCore.QRect(334, 0, 81, 61))
self.label_8.setStyleSheet("image: url(:/password.png);")
self.label_8.setText("")
self.label_8.setObjectName("label_8")
self.line_8 = QtWidgets.QFrame(self.centralwidget)
self.line_8.setGeometry(QtCore.QRect(19, 90, 511, 20))
self.line_8.setFrameShape(QtWidgets.QFrame.HLine)
self.line_8.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_8.setObjectName("line_8")
self.line_4 = QtWidgets.QFrame(self.centralwidget)
self.line_4.setGeometry(QtCore.QRect(19, 364, 361, 16))
self.line_4.setFrameShape(QtWidgets.QFrame.HLine)
self.line_4.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_4.setObjectName("line_4")
self.line_7 = QtWidgets.QFrame(self.centralwidget)
self.line_7.setGeometry(QtCore.QRect(513, 364, 16, 16))
self.line_7.setFrameShape(QtWidgets.QFrame.HLine)
self.line_7.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_7.setObjectName("line_7")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 553, 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 log_in(self):
conn = sqlite3.connect("master.db")
cur = conn.cursor()
user = self.username.text()
password = self.password.text()
print(user)
print(password)
if user=="" or password=="" :
self.error_lbl.setText( "materuser & masterpassword\nrequired !")
else:
cur.execute("SELECT rowid , *FROM master_database WHERE master_users = ?", (user,))
c=cur.fetchone()
conn.commit()
conn.close()
if c==None:
self.error_lbl.setText( f"No data for {user}\nKindly register for new user !" )
else:
if c[2] == password:
#self.error_lbl.setText("Successfully verified.\nClick to Login")
self.window = QtWidgets.QMainWindow()
self.ui = Ui_Dialog()
self.ui.setupUi(self.window)
self.window.show()
else:
self.error_lbl.setText("Incorrect Password !" )
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_())
My Dialog window file------->'
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main2.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
class Ui_Dialog(object):
def loaddata(self):
connection = sqlite3.connect('master.db')
query = "SELECT * FROM a"
c = connection.execute(query)
self.tableWidget.setRowCount(0)
for row_number , row_data in enumerate(c):
self.tableWidget.insertRow(row_number)
for column_number ,data in enumerate(row_data):
self.tableWidget.setItem(row_number,column_number,QtWidgets.QTableWidgetItem(str(data)))
connection.commit()
connection.close()
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(553, 424)
font = QtGui.QFont()
font.setFamily("Consolas")
Dialog.setFont(font)
Dialog.setStyleSheet("background-color: PowderBlue;bn ")
self.label_4 = QtWidgets.QLabel(Dialog)
self.label_4.setGeometry(QtCore.QRect(36, 112, 151, 31))
font = QtGui.QFont()
font.setFamily("Raleway Black")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(Dialog)
self.label_5.setGeometry(QtCore.QRect(225, 112, 111, 31))
font = QtGui.QFont()
font.setFamily("Raleway Black")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(Dialog)
self.label_6.setGeometry(QtCore.QRect(396, 112, 121, 31))
font = QtGui.QFont()
font.setFamily("Raleway Black")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_6.setFont(font)
self.label_6.setObjectName("label_6")
self.tableWidget = QtWidgets.QTableWidget(Dialog)
self.tableWidget.setGeometry(QtCore.QRect(10, 150, 531, 271))
self.tableWidget.setMinimumSize(QtCore.QSize(531, 271))
self.tableWidget.setMaximumSize(QtCore.QSize(531, 271))
font = QtGui.QFont()
font.setFamily("Source Sans Pro SemiBold")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.tableWidget.setFont(font)
self.tableWidget.setStyleSheet("color: rgb(73, 6, 127);")
self.tableWidget.setFrameShape(QtWidgets.QFrame.Box)
self.tableWidget.setGridStyle(QtCore.Qt.NoPen)
self.tableWidget.setRowCount(10)
self.tableWidget.setColumnCount(3)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.horizontalHeader().setDefaultSectionSize(177)
self.pushButton = QtWidgets.QPushButton(Dialog,clicked=lambda:self.loaddata())
self.pushButton.setGeometry(QtCore.QRect(200, 30, 75, 23))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label_4.setText(_translate("Dialog", "APP/WEBSITE"))
self.label_5.setText(_translate("Dialog", "USERNAME"))
self.label_6.setText(_translate("Dialog", "PASSWORD"))
self.pushButton.setText(_translate("Dialog", "PushButton"))
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_())
I use the QStackedWidget to switch between pages.
app = QApplication(sys.argv)
stackedwidget = QtWidgets.QStackedWidget()
page1 = PageOneClass()
page2 = PageTwoClass()
stackedwidget.addWidget(page1)
stackedwidget.addWidget(page2)
stackedwidget.show()
app.exec_()
This will let you switch between pages without running another file. Now to get values from one page to another, use inheritance.
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.user = self.username.text()
self password = self.password.text() #Now the user, password can be accessed using the class name. dont forget to add self.
class DialogWindow(MainWindow):
def __init__(self, MainWindow):
super(DialogWindow, self).__init__()
usernamefrompage1 = MainWindow.user
passwordfrompage1 = MainWindow.password
app = QApplication(sys.argv)
stackedwidget = QtWidgets.QStackedWidget()
mainwindow_instance = MainWindow()
dialogwindow_instance = DialogWindow(mainwindow_instance)
#we are passing the mainwindow instance to the dialog window instance, this will let us use all the methods and attributes of the mainwindow.
stackedwidget.addWidget(mainwindow_instance)
stackedwidget.addWidget(dialogwindow_instance)
stackedwidget.setCurrentWidget(mainwindow_instance)
stackedwidget.show()
app.exec_()

How to close a window in Pyqt5 [duplicate]

This question already has answers here:
QtDesigner changes will be lost after redesign User Interface
(2 answers)
How to close a QDialog
(4 answers)
Closed 1 year ago.
I've created a program that allows you to login and sign up. When you have the right credentials it opens a window but it doesn't close. The same thing happens when you press the button to sign up. I've tried to use the close method but it doesn't work i always get:
AttributeError: type object 'Ui_Dialog' has no attribute 'close'
here is the full code of all the windows:
Login Page
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QMessageBox
import sqlite3
from welcome import Ui_MainWindow
from signup_page import Ui_Dialog2
import sys
#widget.setCurrentIndex(widget.currentIndex)
class Ui_Dialog(object):
def showMessageBox(self):
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setStyleSheet("color: rgb(0, 0, 0);\n"
"background-color: rgb(255, 0, 0);")
self.label.setObjectName("label")
self.label.setText("WRONG USERNAME/PASSWORD")
def loginCheck(self):
username = self.username_input.text()
password = self.password_input.text()
connection = sqlite3.connect("login.db")
result = connection.execute("SELECT * FROM USERS WHERE USERNAME = ? AND PASSWORD = ?", (username, password))
if (len(result.fetchall())) > 0:
self.welcomewindow = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.welcomewindow)
self.welcomewindow.show()
else:
self.showMessageBox()
def singup(self):
self.signup_page = QtWidgets.QDialog()
self.ui = Ui_Dialog2()
self.ui.setupUi(self.signup_page)
self.signup_page.show()
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(591, 457)
self.user_name_label = QtWidgets.QLabel(Dialog)
self.user_name_label.setGeometry(QtCore.QRect(190, 150, 81, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.user_name_label.sizePolicy().hasHeightForWidth())
self.user_name_label.setSizePolicy(sizePolicy)
self.user_name_label.setSizeIncrement(QtCore.QSize(0, 0))
self.user_name_label.setBaseSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setPointSize(13)
self.user_name_label.setFont(font)
self.user_name_label.setObjectName("user_name_label")
self.password_label = QtWidgets.QLabel(Dialog)
self.password_label.setGeometry(QtCore.QRect(190, 210, 81, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.password_label.sizePolicy().hasHeightForWidth())
self.password_label.setSizePolicy(sizePolicy)
self.password_label.setSizeIncrement(QtCore.QSize(0, 0))
self.password_label.setBaseSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setPointSize(13)
self.password_label.setFont(font)
self.password_label.setObjectName("password_label")
self.username_input = QtWidgets.QLineEdit(Dialog)
self.username_input.setGeometry(QtCore.QRect(280, 150, 141, 20))
self.username_input.setObjectName("username_input")
self.password_input = QtWidgets.QLineEdit(Dialog)
self.password_input.setGeometry(QtCore.QRect(280, 210, 141, 20))
self.password_input.setObjectName("password_input")
self.password_input.setEchoMode(self.password_input.Password)
self.login_btn = QtWidgets.QPushButton(Dialog)
self.login_btn.setGeometry(QtCore.QRect(200, 260, 81, 31))
font = QtGui.QFont()
font.setPointSize(13)
self.login_btn.setFont(font)
self.login_btn.setObjectName("login_btn")
#########Button Event##########
self.login_btn.clicked.connect(self.loginCheck)
###############################
self.signup_btn = QtWidgets.QPushButton(Dialog)
self.signup_btn.setGeometry(QtCore.QRect(300, 260, 81, 31))
########Button Event##########
self.signup_btn.clicked.connect(self.singup)
##############################
font = QtGui.QFont()
font.setPointSize(13)
self.signup_btn.setFont(font)
self.signup_btn.setObjectName("signup_btn")
self.login_title = QtWidgets.QLabel(Dialog)
self.login_title.setGeometry(QtCore.QRect(240, 40, 91, 71))
font = QtGui.QFont()
font.setPointSize(20)
self.login_title.setFont(font)
self.login_title.setObjectName("login_title")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(130, 370, 391, 31))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Login Page"))
self.user_name_label.setText(_translate("Dialog", "Username"))
self.password_label.setText(_translate("Dialog", "Password"))
self.login_btn.setText(_translate("Dialog", "Login"))
self.signup_btn.setText(_translate("Dialog", "Sign Up"))
self.login_title.setText(_translate("Dialog", " Login"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QStackedWidget()
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Dialog()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The main window:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(593, 458)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(200, 180, 221, 71))
font = QtGui.QFont()
font.setPointSize(40)
self.label.setFont(font)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
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.label.setText(_translate("MainWindow", "Welcome"))
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_())
and the signup page:
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
from welcome import Ui_MainWindow
import re
from PyQt5.QtWidgets import QMessageBox
class Ui_Dialog2(object):
def showMessageBox_password(self):
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setStyleSheet("color: rgb(0, 0, 0);\n"
"background-color: rgb(255, 0, 0);")
self.label.setObjectName("label")
self.label.setText("INVALID PASSWORD")
def showMessageBox_email(self):
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setStyleSheet("color: rgb(0, 0, 0);\n"
"background-color: rgb(255, 0, 0);")
self.label.setObjectName("label")
self.label.setText(" INVALID EMAIL")
def insertData(self):
username = self.username_entry.text()
password = self.password_entry.text()
email = self.email_entry.text()
connection = sqlite3.connect("login.db")
regex = '^(\w|\.|\_|\-)+[#](\w|\_|\-|\.)+[.]\w{2,3}$'
valid_email = True
valid_password = True
if(re.search(regex, email)):
valid_email = True
else:
valid_email = False
if len(password) > 7:
valid_password = True
else:
valid_password = False
if valid_email == True and valid_password == True:
connection.execute("INSERT INTO USERS VALUES(?,?,?)", (username, email, password))
connection.commit()
self.welcomewindow = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.welcomewindow)
self.welcomewindow.show()
if valid_email == False:
self.showMessageBox_email()
elif valid_password == False:
self.showMessageBox_password()
connection.close()
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(593, 458)
self.user_name_label = QtWidgets.QLabel(Dialog)
self.user_name_label.setGeometry(QtCore.QRect(190, 200, 81, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.user_name_label.sizePolicy().hasHeightForWidth())
self.user_name_label.setSizePolicy(sizePolicy)
self.user_name_label.setSizeIncrement(QtCore.QSize(0, 0))
self.user_name_label.setBaseSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setPointSize(13)
self.user_name_label.setFont(font)
self.user_name_label.setObjectName("user_name_label")
self.password_label = QtWidgets.QLabel(Dialog)
self.password_label.setGeometry(QtCore.QRect(190, 240, 81, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.password_label.sizePolicy().hasHeightForWidth())
self.password_label.setSizePolicy(sizePolicy)
self.password_label.setSizeIncrement(QtCore.QSize(0, 0))
self.password_label.setBaseSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setPointSize(13)
self.password_label.setFont(font)
self.password_label.setObjectName("password_label")
self.email_label = QtWidgets.QLabel(Dialog)
self.email_label.setGeometry(QtCore.QRect(190, 160, 81, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.email_label.sizePolicy().hasHeightForWidth())
self.email_label.setSizePolicy(sizePolicy)
self.email_label.setSizeIncrement(QtCore.QSize(0, 0))
self.email_label.setBaseSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setPointSize(13)
self.email_label.setFont(font)
self.email_label.setObjectName("email_label")
self.email_entry = QtWidgets.QLineEdit(Dialog)
self.email_entry.setGeometry(QtCore.QRect(270, 160, 200, 20))
self.email_entry.setObjectName("email_entry")
self.username_entry = QtWidgets.QLineEdit(Dialog)
self.username_entry.setGeometry(QtCore.QRect(270, 200, 200, 20))
self.username_entry.setObjectName("username_entry")
self.password_entry = QtWidgets.QLineEdit(Dialog)
self.password_entry.setGeometry(QtCore.QRect(270, 240, 200, 20))
self.password_entry.setObjectName("password_entry")
self.password_entry.setEchoMode(self.password_entry.Password)
self.sign_up_button = QtWidgets.QPushButton(Dialog)
self.sign_up_button.setGeometry(QtCore.QRect(270, 280, 81, 31))
font = QtGui.QFont()
font.setPointSize(13)
self.sign_up_button.setFont(font)
self.sign_up_button.setObjectName("sign_up_button")
self.sign_up_button.clicked.connect(self.insertData)
self.login_title = QtWidgets.QLabel(Dialog)
self.login_title.setGeometry(QtCore.QRect(220, 50, 191, 71))
font = QtGui.QFont()
font.setPointSize(20)
self.login_title.setFont(font)
self.login_title.setObjectName("login_title")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(170, 360, 251, 31))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Sign Up Page"))
self.user_name_label.setText(_translate("Dialog", "Username"))
self.password_label.setText(_translate("Dialog", "Password"))
self.email_label.setText(_translate("Dialog", "Email"))
self.sign_up_button.setText(_translate("Dialog", "Sign Up"))
self.login_title.setText(_translate("Dialog", "Create Account"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Dialog2()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

PYQT5: How to link the 3 pushbottons with 3 .py

How to use pyqtSlot to connect the QMainWindow with 3 QtWidgets?
I wanna to use 3 buttons to switch 3 screen for each .py files.
The UiPE.py file for the 3 pushbutton to switch 3 screens of .py file.
#=============
**UiPE.py**
#=============
# -*- coding: utf-8 -*-
"""
Module implementing PB01.
"""
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow
from Ui_UiPE import Ui_MainWindow
class PB01(QMainWindow, Ui_MainWindow):
"""
Class documentation goes here.
"""
def __init__(self, parent=None):
"""
Constructor
#param parent reference to the parent widget
#type QWidget
"""
super(PB01, self).__init__(parent)
self.setupUi(self)
#pyqtSlot()
def on_PBtn1PE_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
#raise NotImplementedError
#pyqtSlot()
def on_PBtn2LC_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
raise NotImplementedError
#pyqtSlot()
def on_PBtn3PP_clicked(self):
"""
Slot documentation goes here.
"""
# TODO: not implemented yet
raise NotImplementedError
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
FormPE = QtWidgets.QWidget()
ui = Ui_FormPE()
ui.setupUi(FormPE)
FormPE.show()
sys.exit(app.exec_())
Ui_UiPE.py
#=============
**Ui_UiPE.py**
#=============
# -*- coding: utf-8 -*-
/*Form implementation generated from reading ui file
'..\PyQt5\eee\UiPE.ui'*/
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(960, 736)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setStyleSheet("border-image:\
url(:/ME/images/Main.jpg);")
self.centralWidget.setObjectName("centralWidget")
self.frame = QtWidgets.QFrame(self.centralWidget)
self.frame.setGeometry(QtCore.QRect(0, 0, 960, 500))
self.frame.setAutoFillBackground(False)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.PBtn1PE = QtWidgets.QPushButton(self.frame)
self.PBtn1PE.setEnabled(True)
self.PBtn1PE.setGeometry(QtCore.QRect(150, 400, 150, 50))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(False)
font.setWeight(50)
self.PBtn1PE.setFont(font)
self.PBtn1PE.setStyleSheet("border:2px groove gray;\
border-radius:10px;padding:2px 4px;background-color: \
red;border-image: url();")
self.PBtn1PE.setText("PE")
self.PBtn1PE.setDefault(False)
self.PBtn1PE.setObjectName("PBtn1PE")
self.PBtn2LC = QtWidgets.QPushButton(self.frame)
self.PBtn2LC.setGeometry(QtCore.QRect(400, 400, 150, 50))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(False)
font.setWeight(50)
self.PBtn2LC.setFont(font)
self.PBtn2LC.setStyleSheet("border:2px groove gray;\
border-radius:10px;padding:2px 4px;background-color: \
yellow;border-image: url();")
self.PBtn2LC.setObjectName("PBtn2LC")
self.PBtn3PP = QtWidgets.QPushButton(self.frame)
self.PBtn3PP.setGeometry(QtCore.QRect(650, 400, 150, 50))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(False)
font.setWeight(50)
self.PBtn3PP.setFont(font)
self.PBtn3PP.setAutoFillBackground(False)
self.PBtn3PP.setStyleSheet("border:2px groove gray;\
border-radius:10px;padding:2px 4px;background-color: \
rgb(0, 162, 232);;border-image: url();")
self.PBtn3PP.setObjectName("PBtn3PP")
self.BtnClose = QtWidgets.QPushButton(self.frame)
self.BtnClose.setGeometry(QtCore.QRect(860, 40, 81, 41))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.BtnClose.setFont(font)
self.BtnClose.setWhatsThis("")
self.BtnClose.setAccessibleName("")
self.BtnClose.setAutoFillBackground(False)
self.BtnClose.setStyleSheet("border:2px groove gray;\
border-radius:10px;padding:2px 4px;background-color: red;\
border-image: url();")
self.BtnClose.setText("Exit")
self.BtnClose.setObjectName("BtnClose")
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
self.PBtn1PE.released.connect(MainWindow.show)
self.BtnClose.clicked.connect(self.frame.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.PBtn1PE, self.PBtn2LC)
MainWindow.setTabOrder(self.PBtn2LC, self.PBtn3PP)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.PBtn2LC.setText(_translate("MainWindow", "LC"))
self.PBtn3PP.setText(_translate("MainWindow", "PP"))
#import MainE_rc
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_())
Ui_UiPE1.py
/*=============
#**Ui_UiPE1.py**, **Ui_UiLC.py** and **Ui_UiPP.py** is same script for
#differet function.
=============*/
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file \
#'C:\Users\ST3C_01\PyQt5\eee\UiPE1.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_FormPE(object):
def setupUi(self, FormPE):
FormPE.setObjectName("FormPE")
FormPE.resize(960, 500)
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
FormPE.setFont(font)
FormPE.setStyleSheet("border-image:\
url(:/myframe/images/PresidentElection.jpg);")
self.PEBtn1B = QtWidgets.QPushButton(FormPE)
self.PEBtn1B.setGeometry(QtCore.QRect(540, 420, 111, 40))
self.PEBtn1B.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.PEBtn1B.setFont(font)
self.PEBtn1B.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn1B.setText(" Decrease")
self.PEBtn1B.setObjectName("PEBtn1B")
self.PEBtn1A = QtWidgets.QPushButton(FormPE)
self.PEBtn1A.setGeometry(QtCore.QRect(550, 30, 111, 40))
self.PEBtn1A.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.PEBtn1A.setFont(font)
self.PEBtn1A.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn1A.setText(" Increase")
self.PEBtn1A.setObjectName("PEBtn1A")
self.PEBtn2A = QtWidgets.QPushButton(FormPE)
self.PEBtn2A.setGeometry(QtCore.QRect(690, 30, 111, 40))
self.PEBtn2A.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.PEBtn2A.setFont(font)
self.PEBtn2A.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn2A.setText(" Increase")
self.PEBtn2A.setObjectName("PEBtn2A")
self.PEBtn3A = QtWidgets.QPushButton(FormPE)
self.PEBtn3A.setGeometry(QtCore.QRect(830, 30, 111, 40))
self.PEBtn3A.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.PEBtn3A.setFont(font)
self.PEBtn3A.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn3A.setText(" Increase")
self.PEBtn3A.setObjectName("PEBtn3A")
self.PEBtn2B = QtWidgets.QPushButton(FormPE)
self.PEBtn2B.setGeometry(QtCore.QRect(690, 420, 111, 40))
self.PEBtn2B.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.PEBtn2B.setFont(font)
self.PEBtn2B.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn2B.setText(" Decrease")
self.PEBtn2B.setObjectName("PEBtn2B")
self.PEBtn3B = QtWidgets.QPushButton(FormPE)
self.PEBtn3B.setGeometry(QtCore.QRect(830, 420, 111, 40))
self.PEBtn3B.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.PEBtn3B.setFont(font)
self.PEBtn3B.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn3B.setText(" Decrease")
self.PEBtn3B.setObjectName("PEBtn3B")
self.retranslateUi(FormPE)
QtCore.QMetaObject.connectSlotsByName(FormPE)
def retranslateUi(self, FormPE):
_translate = QtCore.QCoreApplication.translate
FormPE.setWindowTitle(_translate("FormPE", "Form"))
#import PE_rc
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
FormPE = QtWidgets.QWidget()
ui = Ui_FormPE()
ui.setupUi(FormPE)
FormPE.show()
sys.exit(app.exec_())
The above is my example.
Try it:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
# MainWindow.resize(960, 536)
MainWindow.setFixedSize(960, 470) # +
self.centralWidget = QtWidgets.QWidget(MainWindow)
# self.centralWidget.setStyleSheet("border-image: url(Ok.png);")
self.centralWidget.setObjectName("centralWidget") # +++
self.centralWidget.setStyleSheet("#centralWidget {border-image: url(Ok.png);}") # +++
self.frame = QtWidgets.QFrame(self.centralWidget)
self.frame.setGeometry(QtCore.QRect(0, 0, 960, 500))
self.frame.setAutoFillBackground(False)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.PBtn1PE = QtWidgets.QPushButton(self.frame)
self.PBtn1PE.setEnabled(True)
self.PBtn1PE.setGeometry(QtCore.QRect(150, 400, 150, 50))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(False)
font.setWeight(50)
self.PBtn1PE.setFont(font)
self.PBtn1PE.setStyleSheet("""
border: 2px groove gray;
border-radius: 10px;
padding: 2px 4px;
background-color: red;
""")
self.PBtn1PE.setText("PE")
self.PBtn1PE.setDefault(False)
self.PBtn1PE.setObjectName("PBtn1PE")
self.PBtn2LC = QtWidgets.QPushButton(self.frame)
self.PBtn2LC.setGeometry(QtCore.QRect(400, 400, 150, 50))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(False)
font.setWeight(50)
self.PBtn2LC.setFont(font)
self.PBtn2LC.setStyleSheet("border:2px groove gray;\
border-radius:10px;padding:2px 4px;background-color: yellow;")
self.PBtn2LC.setObjectName("PBtn2LC")
self.PBtn3PP = QtWidgets.QPushButton(self.frame)
self.PBtn3PP.setGeometry(QtCore.QRect(650, 400, 150, 50))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(18)
font.setBold(False)
font.setWeight(50)
self.PBtn3PP.setFont(font)
self.PBtn3PP.setAutoFillBackground(False)
self.PBtn3PP.setStyleSheet("border:2px groove gray;\
border-radius:10px;padding:2px 4px;background-color: rgb(0, 162, 232);")
self.PBtn3PP.setObjectName("PBtn3PP")
self.BtnClose = QtWidgets.QPushButton(self.frame)
self.BtnClose.setGeometry(QtCore.QRect(860, 40, 81, 41))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.BtnClose.setFont(font)
self.BtnClose.setWhatsThis("")
self.BtnClose.setAccessibleName("")
self.BtnClose.setAutoFillBackground(False)
self.BtnClose.setStyleSheet("border:2px groove gray;\
border-radius:10px;padding:2px 4px;background-color: red;")
self.BtnClose.setText("Exit")
self.BtnClose.setObjectName("BtnClose")
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
self.PBtn1PE.released.connect(MainWindow.show)
self.BtnClose.clicked.connect(self.frame.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.PBtn1PE, self.PBtn2LC)
MainWindow.setTabOrder(self.PBtn2LC, self.PBtn3PP)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.PBtn2LC.setText(_translate("MainWindow", "LC"))
self.PBtn3PP.setText(_translate("MainWindow", "PP"))
class Ui_FormPE(object):
def setupUi(self, FormPE):
FormPE.setObjectName("FormPE")
FormPE.resize(960, 500)
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
FormPE.setFont(font)
FormPE.setStyleSheet("#FormPE {border-image: url(im.png);}") # + #FormPE
self.PEBtn1B = QtWidgets.QPushButton(FormPE)
self.PEBtn1B.setGeometry(QtCore.QRect(540, 420, 111, 40))
self.PEBtn1B.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16) # 16
font.setBold(True)
font.setWeight(75)
self.PEBtn1B.setFont(font)
self.PEBtn1B.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn1B.setText(" Decrease ") # " Decrease "
self.PEBtn1B.setObjectName("PEBtn1B")
self.PEBtn1A = QtWidgets.QPushButton(FormPE)
self.PEBtn1A.setGeometry(QtCore.QRect(550, 30, 111, 40))
self.PEBtn1A.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.PEBtn1A.setFont(font)
self.PEBtn1A.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn1A.setText(" Increase ")
self.PEBtn1A.setObjectName("PEBtn1A")
self.PEBtn2A = QtWidgets.QPushButton(FormPE)
self.PEBtn2A.setGeometry(QtCore.QRect(690, 30, 111, 40))
self.PEBtn2A.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.PEBtn2A.setFont(font)
self.PEBtn2A.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn2A.setText(" Increase ")
self.PEBtn2A.setObjectName("PEBtn2A")
self.PEBtn3A = QtWidgets.QPushButton(FormPE)
self.PEBtn3A.setGeometry(QtCore.QRect(830, 30, 111, 40))
self.PEBtn3A.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.PEBtn3A.setFont(font)
self.PEBtn3A.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn3A.setText(" Increase ")
self.PEBtn3A.setObjectName("PEBtn3A")
self.PEBtn2B = QtWidgets.QPushButton(FormPE)
self.PEBtn2B.setGeometry(QtCore.QRect(690, 420, 111, 40))
self.PEBtn2B.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.PEBtn2B.setFont(font)
self.PEBtn2B.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn2B.setText(" Decrease ")
self.PEBtn2B.setObjectName("PEBtn2B")
self.PEBtn3B = QtWidgets.QPushButton(FormPE)
self.PEBtn3B.setGeometry(QtCore.QRect(830, 420, 111, 40))
self.PEBtn3B.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("MingLi")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.PEBtn3B.setFont(font)
self.PEBtn3B.setLayoutDirection(QtCore.Qt.RightToLeft)
self.PEBtn3B.setText(" Decrease ")
self.PEBtn3B.setObjectName("PEBtn3B")
self.retranslateUi(FormPE)
QtCore.QMetaObject.connectSlotsByName(FormPE)
def retranslateUi(self, FormPE):
_translate = QtCore.QCoreApplication.translate
FormPE.setWindowTitle(_translate("FormPE", "Form"))
class PB01(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(PB01, self).__init__(parent)
self.setupUi(self)
#pyqtSlot()
def on_PBtn1PE_clicked(self):
print("on_PBtn1PE_clicked")
self.FormPE = QtWidgets.QWidget()
ui = Ui_FormPE()
ui.setupUi(self.FormPE)
self.FormPE.show()
#pyqtSlot()
def on_PBtn2LC_clicked(self):
print("on_PBtn2LC_clicked")
#pyqtSlot()
def on_PBtn3PP_clicked(self):
print("on_PBtn3PP_clicked")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = PB01() # +++
w.show() # +++
# FormPE = QtWidgets.QWidget()
# ui = Ui_FormPE()
# ui.setupUi(FormPE)
# FormPE.show()
sys.exit(app.exec_())

QObject::setParent: Cannot set parent, new parent is in a different thread in Python

I am facing a problem with the GUI I developed using PyQt5.
The app tries to update the value of a progress bar of the window from a different thread (QThreadPool()). When it tries to update the progress bar, the python gives a warning as below:
QObject::setParent: Cannot set parent, new parent is in a different thread
This doesn't affect the app to certain extent but after a while the app crashes with the following error message.
QBackingStore::endPaint() called with active painter on backingstore paint device
Could anyone help me to get rid of the first warning? I guess that will solve the problem.
I will post a minimal example of the code.
main.py
import sys
from PyQt5.QtCore import *
from PyQt5 import QtCore, QtGui, QtWidgets
import thread_for_audio_record
import time
class FW(object):
def __init__(self):
self.threadpool = QThreadPool()
def f_w(self, window):
self.window = window
self.window.setObjectName("Test")
self.window.resize(1054, 700)
self.window.setStyleSheet("background-color: rgb(180, 180, 180);")
self.central_widget = QtWidgets.QWidget(self.window)
self.central_widget.setObjectName("centralwidget")
self.horizontal_layout = QtWidgets.QHBoxLayout(self.central_widget)
self.horizontal_layout.setObjectName("horizontalLayout")
spacer_item = QtWidgets.QSpacerItem(129, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontal_layout.addItem(spacer_item)
self.vertical_layout = QtWidgets.QVBoxLayout()
self.vertical_layout.setObjectName("verticalLayout")
spacer_item_1 = QtWidgets.QSpacerItem(128, 13, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.vertical_layout.addItem(spacer_item_1)
self.label_utterance = QtWidgets.QLabel(self.central_widget)
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setBold(True)
font.setWeight(75)
self.label_utterance.setFont(font)
self.label_utterance.setAlignment(QtCore.Qt.AlignCenter)
self.label_utterance.setObjectName("label_utterance")
self.vertical_layout.addWidget(self.label_utterance)
self.text_edit_utterance = QtWidgets.QTextEdit(self.central_widget)
self.text_edit_utterance.setStyleSheet("background-color: rgb(255, 255, 255);")
self.text_edit_utterance.setObjectName("text_edit_utterance")
self.text_edit_utterance.setReadOnly(True)
self.text_edit_utterance.setFont(font)
self.vertical_layout.addWidget(self.text_edit_utterance)
self.line_edit_message_for_user = QtWidgets.QLineEdit(self.central_widget)
self.line_edit_message_for_user.setStyleSheet("background-color: rgb(255, 255, 255);")
self.line_edit_message_for_user.setObjectName("lineEdit")
self.progressBar = QtWidgets.QProgressBar(self.central_widget)
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName("progressBar")
self.vertical_layout.addWidget(self.progressBar)
self.vertical_layout.addWidget(self.line_edit_message_for_user)
self.push_button_start_recording = QtWidgets.QPushButton(self.central_widget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.push_button_start_recording.setFont(font)
self.push_button_start_recording.setStyleSheet("background-color: rgb(85, 170, 0);")
self.push_button_start_recording.setObjectName("pushButton_start_
recording")
self.push_button_start_recording.clicked.connect(self.start_button_
func)
self.vertical_layout.addWidget(self.push_button_start_recording)
self.push_button_end_recording = QtWidgets.QPushButton(self.central_widget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.push_button_end_recording.setFont(font)
self.push_button_end_recording.setStyleSheet("background-color: rgb(182, 0, 0);")
self.push_button_end_recording.setObjectName("pushButton_end_
recording")
self.vertical_layout.addWidget(self.push_button_end_recording)
self.label_status = QtWidgets.QLabel(self.central_widget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_status.setFont(font)
self.label_status.setObjectName("label_status")
self.vertical_layout.addWidget(self.label_status)
self.text_edit_status = QtWidgets.QTextEdit(self.central_widget)
self.text_edit_status.setStyleSheet("background-color: rgb(255, 255, 255);")
self.text_edit_status.setObjectName("textEdit_status")
self.text_edit_status.setReadOnly(True)
self.vertical_layout.addWidget(self.text_edit_status)
self.push_button_record_again = QtWidgets.QPushButton(self.central_widget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.push_button_record_again.setFont(font)
self.push_button_record_again.setStyleSheet("background-color: rgb(255, 255, 127);")
self.push_button_record_again.setObjectName("pushButton_record_
again")
self.vertical_layout.addWidget(self.push_button_record_again)
self.push_button_next_utterance = QtWidgets.QPushButton(self.central_widget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.push_button_next_utterance.setFont(font)
self.push_button_next_utterance.setStyleSheet("background-color: rgb(85, 170, 0);")
self.push_button_next_utterance.setObjectName("pushButton_next_
utterance")
self.vertical_layout.addWidget(self.push_button_next_utterance)
spacer_item_2 = QtWidgets.QSpacerItem(128, 13, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.vertical_layout.addItem(spacer_item_2)
self.horizontal_layout.addLayout(self.vertical_layout)
spacer_item_3 = QtWidgets.QSpacerItem(128, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontal_layout.addItem(spacer_item_3)
self.window.setCentralWidget(self.central_widget)
self.menubar = QtWidgets.QMenuBar(self.window)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1054, 21))
self.menubar.setObjectName("menubar")
self.window.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(self.window)
self.statusbar.setObjectName("statusbar")
self.window.setStatusBar(self.statusbar)
self.retranslate_ui(self.window)
QtCore.QMetaObject.connectSlotsByName(self.window)
def retranslate_ui(self, window):
_translate = QtCore.QCoreApplication.translate
window.setWindowTitle(_translate("Utterance Recording", "Test"))
self.label_utterance.setText(_translate("Utterance Recording", "Test"))
self.line_edit_message_for_user.setText(_translate("Utterance Recording", "Test."))
self.text_edit_utterance.setText(_translate("utterance_parsing", ('Test')))
self.push_button_start_recording.setText(_translate("Utterance Recording", "Start Recording"))
self.push_button_end_recording.setText(_translate("Utterance Recording", "End Recording"))
self.label_status.setText(_translate("Utterance Recording", "Test"))
self.push_button_record_again.setText(_translate("Utterance Recording", "Test"))
self.push_button_next_utterance.setText(_translate("Utterance Recording", "Test"))
def inside_thread(self):
percentage = 0
while percentage < 120:
percentage = percentage + 20
time.sleep(0.2)
self.progressBar.setValue(percentage)
def start_button_func(self):
self.worker = thread_for_audio_record.Worker(self.inside_thread)
self.threadpool.start(self.worker)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
utterance_parsing_window = QtWidgets.QMainWindow()
utterance_parsing = FW()
utterance_parsing.f_w(utterance_parsing_window)
utterance_parsing_window.show()
sys.exit(app.exec_())
thread_for_audio_record.py
import os
from PyQt5.QtCore import *
class Worker(QRunnable):
def __init__(self, fn):
super(Worker, self).__init__()
self.fn = fn
self.originating_PID = os.getpid()
self._running = True
#pyqtSlot()
def run(self):
self.fn()
Click the Start Recording button and you will see the progress bar getting updated. But the warning message is given cause the update is from a different thread.
First, do not modify the code generated by Qt Designer as PyQt recommends, instead create another class that inherits from the appropriate widget and use the initial class as an interface.
Going to the point, Qt does not allow the GUI to be updated from another thread directly, in your case the setValue of the QProgressBar is called in the secondary thread causing the painting to be made in the secuandario thread so Qt complains that it is not guaranteed operation (for example in your case it does not generate problems apparently but Qt does not guarantee that this always happens). The update of the GUI from other threads must be indirectly, which can be through signals, QEvent, QMetaObject::invokeMethod, etc. In this case I will use the signals:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import thread_for_audio_record
import time
class FW(object):
def f_w(self, window):
self.window = window
self.window.setObjectName("Test")
self.window.resize(1054, 700)
self.window.setStyleSheet("background-color: rgb(180, 180, 180);")
self.central_widget = QtWidgets.QWidget(self.window)
self.central_widget.setObjectName("centralwidget")
self.horizontal_layout = QtWidgets.QHBoxLayout(self.central_widget)
self.horizontal_layout.setObjectName("horizontalLayout")
spacer_item = QtWidgets.QSpacerItem(129, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontal_layout.addItem(spacer_item)
self.vertical_layout = QtWidgets.QVBoxLayout()
self.vertical_layout.setObjectName("verticalLayout")
spacer_item_1 = QtWidgets.QSpacerItem(128, 13, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.vertical_layout.addItem(spacer_item_1)
self.label_utterance = QtWidgets.QLabel(self.central_widget)
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setBold(True)
font.setWeight(75)
self.label_utterance.setFont(font)
self.label_utterance.setAlignment(QtCore.Qt.AlignCenter)
self.label_utterance.setObjectName("label_utterance")
self.vertical_layout.addWidget(self.label_utterance)
self.text_edit_utterance = QtWidgets.QTextEdit(self.central_widget)
self.text_edit_utterance.setStyleSheet("background-color: rgb(255, 255, 255);")
self.text_edit_utterance.setObjectName("text_edit_utterance")
self.text_edit_utterance.setReadOnly(True)
self.text_edit_utterance.setFont(font)
self.vertical_layout.addWidget(self.text_edit_utterance)
self.line_edit_message_for_user = QtWidgets.QLineEdit(self.central_widget)
self.line_edit_message_for_user.setStyleSheet("background-color: rgb(255, 255, 255);")
self.line_edit_message_for_user.setObjectName("lineEdit")
self.progressBar = QtWidgets.QProgressBar(self.central_widget)
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName("progressBar")
self.vertical_layout.addWidget(self.progressBar)
self.vertical_layout.addWidget(self.line_edit_message_for_user)
self.push_button_start_recording = QtWidgets.QPushButton(self.central_widget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.push_button_start_recording.setFont(font)
self.push_button_start_recording.setStyleSheet("background-color: rgb(85, 170, 0);")
self.push_button_start_recording.setObjectName("pushButton_start_ recording")
self.vertical_layout.addWidget(self.push_button_start_recording)
self.push_button_end_recording = QtWidgets.QPushButton(self.central_widget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.push_button_end_recording.setFont(font)
self.push_button_end_recording.setStyleSheet("background-color: rgb(182, 0, 0);")
self.push_button_end_recording.setObjectName("pushButton_end_recording")
self.vertical_layout.addWidget(self.push_button_end_recording)
self.label_status = QtWidgets.QLabel(self.central_widget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_status.setFont(font)
self.label_status.setObjectName("label_status")
self.vertical_layout.addWidget(self.label_status)
self.text_edit_status = QtWidgets.QTextEdit(self.central_widget)
self.text_edit_status.setStyleSheet("background-color: rgb(255, 255, 255);")
self.text_edit_status.setObjectName("textEdit_status")
self.text_edit_status.setReadOnly(True)
self.vertical_layout.addWidget(self.text_edit_status)
self.push_button_record_again = QtWidgets.QPushButton(self.central_widget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.push_button_record_again.setFont(font)
self.push_button_record_again.setStyleSheet("background-color: rgb(255, 255, 127);")
self.push_button_record_again.setObjectName("pushButton_record_again")
self.vertical_layout.addWidget(self.push_button_record_again)
self.push_button_next_utterance = QtWidgets.QPushButton(self.central_widget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.push_button_next_utterance.setFont(font)
self.push_button_next_utterance.setStyleSheet("background-color: rgb(85, 170, 0);")
self.push_button_next_utterance.setObjectName("pushButton_next_utterance")
self.vertical_layout.addWidget(self.push_button_next_utterance)
spacer_item_2 = QtWidgets.QSpacerItem(128, 13, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.vertical_layout.addItem(spacer_item_2)
self.horizontal_layout.addLayout(self.vertical_layout)
spacer_item_3 = QtWidgets.QSpacerItem(128, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontal_layout.addItem(spacer_item_3)
self.window.setCentralWidget(self.central_widget)
self.menubar = QtWidgets.QMenuBar(self.window)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1054, 21))
self.menubar.setObjectName("menubar")
self.window.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(self.window)
self.statusbar.setObjectName("statusbar")
self.window.setStatusBar(self.statusbar)
self.retranslate_ui(self.window)
QtCore.QMetaObject.connectSlotsByName(self.window)
def retranslate_ui(self, window):
_translate = QtCore.QCoreApplication.translate
window.setWindowTitle(_translate("Utterance Recording", "Altran Audio Manager"))
self.label_utterance.setText(_translate("Utterance Recording", "Test"))
self.line_edit_message_for_user.setText(_translate("Utterance Recording", "Test."))
self.text_edit_utterance.setText(_translate("utterance_parsing", ('Test')))
self.push_button_start_recording.setText(_translate("Utterance Recording", "Start Recording"))
self.push_button_end_recording.setText(_translate("Utterance Recording", "End Recording"))
self.label_status.setText(_translate("Utterance Recording", "Test"))
self.push_button_record_again.setText(_translate("Utterance Recording", "Test"))
self.push_button_next_utterance.setText(_translate("Utterance Recording", "Test"))
class MainWindow(QtWidgets.QMainWindow, FW):
progressChanged = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.threadpool = QtCore.QThreadPool()
self.f_w(self)
self.push_button_start_recording.clicked.connect(self.start_button_func)
self.progressChanged.connect(self.progressBar.setValue)
def inside_thread(self):
percentage = 0
while percentage < 120:
percentage = percentage + 20
time.sleep(0.2)
self.progressChanged.emit(percentage)
def start_button_func(self):
self.worker = thread_for_audio_record.Worker(self.inside_thread)
self.threadpool.start(self.worker)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

Linking PyQt buttons with Python actions

I'm having trouble getting buttons in a GUI laid out in QT Designer and converted into a python script to do anything when clicked.
At the moment my code looks like this:
import sys
from PyQt4.QtGui import QApplication, QDialog
from punchmenuui import *
from PyQt4.QtGui import *
from PyQt4.QtCore import pyqtSignal
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.closemenubutton.clicked.connect(self.closemenu)
def closemenu(self):
sys.exit()
app = QApplication(sys.argv)
window = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(window)
window.show()
sys.exit(app.exec_())
As you can see, its simple. I have a button specified in the layout .py file and I'm trying to capture the "clicked" signal from it and have that trigger a clean exit event. The trouble is: nothing seems to achieve that.
I've tried both the newer and older style PyQt signal/slot bindings to no avail.
Can anyone spot what I'm doing wrong?
EDIT:
Thanks for the responses so far guys. I've tried every combination of all of your answers, but none of them work so far. My current code for the launcher .py script stands as:
import sys
from PyQt4.QtGui import QApplication, QDialog
from punchmenuui import *
from PyQt4.QtGui import *
from PyQt4.QtCore import pyqtSignal
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.closemenubutton.clicked.connect(self.closemenu)
def closemenu():
sys.exit()
app = QApplication(sys.argv)
window = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(window)
window.show()
sys.exit(app.exec_())
and the script for the QT-Designed ui is:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'punchmenu.ui'
#
# Created: Tue Mar 10 01:30:31 2015
# by: PyQt4 UI code generator 4.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(480, 320)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tabWidget = QtGui.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(-1, 0, 481, 301))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.tabWidget.sizePolicy().hasHeightForWidth())
self.tabWidget.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.tabWidget.setFont(font)
self.tabWidget.setStyleSheet(_fromUtf8("QTabBar::scroller { /* the width of the scroll buttons */ width: 50px; } "))
self.tabWidget.setTabPosition(QtGui.QTabWidget.North)
self.tabWidget.setTabShape(QtGui.QTabWidget.Rounded)
self.tabWidget.setElideMode(QtCore.Qt.ElideNone)
self.tabWidget.setUsesScrollButtons(True)
self.tabWidget.setDocumentMode(True)
self.tabWidget.setTabsClosable(False)
self.tabWidget.setMovable(False)
self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
self.hometab = QtGui.QWidget()
self.hometab.setObjectName(_fromUtf8("hometab"))
self.label = QtGui.QLabel(self.hometab)
self.label.setGeometry(QtCore.QRect(60, 0, 371, 231))
self.label.setText(_fromUtf8(""))
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("vtc punch logo.jpg")))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.hometab)
self.label_2.setGeometry(QtCore.QRect(10, 240, 81, 31))
font = QtGui.QFont()
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.flipscreenbutton = QtGui.QPushButton(self.hometab)
self.flipscreenbutton.setGeometry(QtCore.QRect(150, 240, 191, 31))
font = QtGui.QFont()
font.setPointSize(16)
self.flipscreenbutton.setFont(font)
self.flipscreenbutton.setObjectName(_fromUtf8("flipscreenbutton"))
self.tabWidget.addTab(self.hometab, _fromUtf8(""))
self.powertab = QtGui.QWidget()
self.powertab.setObjectName(_fromUtf8("powertab"))
self.groupBox_5 = QtGui.QGroupBox(self.powertab)
self.groupBox_5.setGeometry(QtCore.QRect(10, 10, 461, 261))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.groupBox_5.setFont(font)
self.groupBox_5.setObjectName(_fromUtf8("groupBox_5"))
self.restartbutton = QtGui.QPushButton(self.groupBox_5)
self.restartbutton.setGeometry(QtCore.QRect(130, 20, 181, 111))
font = QtGui.QFont()
font.setPointSize(18)
font.setBold(False)
font.setWeight(50)
self.restartbutton.setFont(font)
self.restartbutton.setObjectName(_fromUtf8("restartbutton"))
self.shutdownbutton = QtGui.QPushButton(self.groupBox_5)
self.shutdownbutton.setGeometry(QtCore.QRect(130, 140, 181, 111))
font = QtGui.QFont()
font.setPointSize(18)
self.shutdownbutton.setFont(font)
self.shutdownbutton.setObjectName(_fromUtf8("shutdownbutton"))
self.tabWidget.addTab(self.powertab, _fromUtf8(""))
self.lanipconfigtab = QtGui.QWidget()
self.lanipconfigtab.setObjectName(_fromUtf8("lanipconfigtab"))
self.groupBox = QtGui.QGroupBox(self.lanipconfigtab)
self.groupBox.setGeometry(QtCore.QRect(10, 10, 461, 261))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.groupBox.setFont(font)
self.groupBox.setObjectName(_fromUtf8("groupBox"))
self.dhcpbutton = QtGui.QPushButton(self.groupBox)
self.dhcpbutton.setGeometry(QtCore.QRect(10, 80, 201, 91))
font = QtGui.QFont()
font.setPointSize(16)
self.dhcpbutton.setFont(font)
self.dhcpbutton.setObjectName(_fromUtf8("dhcpbutton"))
self.staticbutton = QtGui.QPushButton(self.groupBox)
self.staticbutton.setGeometry(QtCore.QRect(250, 80, 201, 91))
font = QtGui.QFont()
font.setPointSize(16)
self.staticbutton.setFont(font)
self.staticbutton.setObjectName(_fromUtf8("staticbutton"))
self.tabWidget.addTab(self.lanipconfigtab, _fromUtf8(""))
self.wificonfigtab = QtGui.QWidget()
self.wificonfigtab.setObjectName(_fromUtf8("wificonfigtab"))
self.scrollArea = QtGui.QScrollArea(self.wificonfigtab)
self.scrollArea.setGeometry(QtCore.QRect(0, 0, 481, 281))
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
self.scrollAreaWidgetContents = QtGui.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 477, 277))
self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
self.groupBox_9 = QtGui.QGroupBox(self.scrollAreaWidgetContents)
self.groupBox_9.setGeometry(QtCore.QRect(0, 10, 471, 261))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.groupBox_9.setFont(font)
self.groupBox_9.setObjectName(_fromUtf8("groupBox_9"))
self.layoutWidget = QtGui.QWidget(self.groupBox_9)
self.layoutWidget.setGeometry(QtCore.QRect(10, 20, 451, 241))
self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
self.verticalLayout = QtGui.QVBoxLayout(self.layoutWidget)
self.verticalLayout.setSizeConstraint(QtGui.QLayout.SetDefaultConstraint)
self.verticalLayout.setMargin(0)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.scanforaccesspointsbutton = QtGui.QPushButton(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(16)
self.scanforaccesspointsbutton.setFont(font)
self.scanforaccesspointsbutton.setObjectName(_fromUtf8("scanforaccesspointsbutton"))
self.verticalLayout.addWidget(self.scanforaccesspointsbutton)
self.connectwifibutton = QtGui.QPushButton(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(16)
self.connectwifibutton.setFont(font)
self.connectwifibutton.setObjectName(_fromUtf8("connectwifibutton"))
self.verticalLayout.addWidget(self.connectwifibutton)
self.disconnectwifibutton = QtGui.QPushButton(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(16)
self.disconnectwifibutton.setFont(font)
self.disconnectwifibutton.setObjectName(_fromUtf8("disconnectwifibutton"))
self.verticalLayout.addWidget(self.disconnectwifibutton)
self.erasestoredwifinetworksbutton = QtGui.QPushButton(self.layoutWidget)
font = QtGui.QFont()
font.setPointSize(16)
self.erasestoredwifinetworksbutton.setFont(font)
self.erasestoredwifinetworksbutton.setObjectName(_fromUtf8("erasestoredwifinetworksbutton"))
self.verticalLayout.addWidget(self.erasestoredwifinetworksbutton)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.tabWidget.addTab(self.wificonfigtab, _fromUtf8(""))
self.captivportallogintab = QtGui.QWidget()
self.captivportallogintab.setObjectName(_fromUtf8("captivportallogintab"))
self.webView = QtWebKit.QWebView(self.captivportallogintab)
self.webView.setGeometry(QtCore.QRect(0, 0, 481, 271))
self.webView.setUrl(QtCore.QUrl(_fromUtf8("http://www.briancourt.co.uk/")))
self.webView.setZoomFactor(0.40000000596)
self.webView.setRenderHints(QtGui.QPainter.SmoothPixmapTransform|QtGui.QPainter.TextAntialiasing)
self.webView.setObjectName(_fromUtf8("webView"))
self.tabWidget.addTab(self.captivportallogintab, _fromUtf8(""))
self.wifiapconfigtab = QtGui.QWidget()
self.wifiapconfigtab.setObjectName(_fromUtf8("wifiapconfigtab"))
self.groupBox_4 = QtGui.QGroupBox(self.wifiapconfigtab)
self.groupBox_4.setGeometry(QtCore.QRect(10, 10, 461, 261))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.groupBox_4.setFont(font)
self.groupBox_4.setObjectName(_fromUtf8("groupBox_4"))
self.tabWidget.addTab(self.wifiapconfigtab, _fromUtf8(""))
self.vpnconfigtab = QtGui.QWidget()
self.vpnconfigtab.setObjectName(_fromUtf8("vpnconfigtab"))
self.groupBox_6 = QtGui.QGroupBox(self.vpnconfigtab)
self.groupBox_6.setGeometry(QtCore.QRect(10, 10, 461, 261))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.groupBox_6.setFont(font)
self.groupBox_6.setObjectName(_fromUtf8("groupBox_6"))
self.disconnectvpnbutton = QtGui.QPushButton(self.groupBox_6)
self.disconnectvpnbutton.setGeometry(QtCore.QRect(10, 150, 211, 91))
font = QtGui.QFont()
font.setPointSize(16)
self.disconnectvpnbutton.setFont(font)
self.disconnectvpnbutton.setObjectName(_fromUtf8("disconnectvpnbutton"))
self.connectvpnbutton = QtGui.QPushButton(self.groupBox_6)
self.connectvpnbutton.setGeometry(QtCore.QRect(240, 150, 211, 91))
font = QtGui.QFont()
font.setPointSize(16)
self.connectvpnbutton.setFont(font)
self.connectvpnbutton.setObjectName(_fromUtf8("connectvpnbutton"))
self.switchtotcpbutton = QtGui.QPushButton(self.groupBox_6)
self.switchtotcpbutton.setGeometry(QtCore.QRect(240, 40, 211, 91))
font = QtGui.QFont()
font.setPointSize(16)
self.switchtotcpbutton.setFont(font)
self.switchtotcpbutton.setObjectName(_fromUtf8("switchtotcpbutton"))
self.switchtoudpbutton = QtGui.QPushButton(self.groupBox_6)
self.switchtoudpbutton.setGeometry(QtCore.QRect(10, 40, 211, 91))
font = QtGui.QFont()
font.setPointSize(16)
self.switchtoudpbutton.setFont(font)
self.switchtoudpbutton.setObjectName(_fromUtf8("switchtoudpbutton"))
self.tabWidget.addTab(self.vpnconfigtab, _fromUtf8(""))
self.statusdisplaytab = QtGui.QWidget()
self.statusdisplaytab.setObjectName(_fromUtf8("statusdisplaytab"))
self.lanipbox = QtGui.QGroupBox(self.statusdisplaytab)
self.lanipbox.setGeometry(QtCore.QRect(10, 0, 461, 71))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.lanipbox.setFont(font)
self.lanipbox.setObjectName(_fromUtf8("lanipbox"))
self.lanipshow = QtGui.QLabel(self.lanipbox)
self.lanipshow.setGeometry(QtCore.QRect(10, 20, 451, 41))
font = QtGui.QFont()
font.setPointSize(16)
self.lanipshow.setFont(font)
self.lanipshow.setAlignment(QtCore.Qt.AlignCenter)
self.lanipshow.setObjectName(_fromUtf8("lanipshow"))
self.groupBox_2 = QtGui.QGroupBox(self.statusdisplaytab)
self.groupBox_2.setGeometry(QtCore.QRect(10, 80, 461, 91))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.groupBox_2.setFont(font)
self.groupBox_2.setObjectName(_fromUtf8("groupBox_2"))
self.wifiipshow = QtGui.QLabel(self.groupBox_2)
self.wifiipshow.setGeometry(QtCore.QRect(10, 30, 451, 41))
font = QtGui.QFont()
font.setPointSize(16)
self.wifiipshow.setFont(font)
self.wifiipshow.setAlignment(QtCore.Qt.AlignCenter)
self.wifiipshow.setObjectName(_fromUtf8("wifiipshow"))
self.groupBox_3 = QtGui.QGroupBox(self.statusdisplaytab)
self.groupBox_3.setGeometry(QtCore.QRect(10, 180, 461, 91))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.groupBox_3.setFont(font)
self.groupBox_3.setObjectName(_fromUtf8("groupBox_3"))
self.vpnipshow = QtGui.QLabel(self.groupBox_3)
self.vpnipshow.setGeometry(QtCore.QRect(10, 30, 451, 41))
font = QtGui.QFont()
font.setPointSize(16)
self.vpnipshow.setFont(font)
self.vpnipshow.setAlignment(QtCore.Qt.AlignCenter)
self.vpnipshow.setObjectName(_fromUtf8("vpnipshow"))
self.tabWidget.addTab(self.statusdisplaytab, _fromUtf8(""))
self.softwareupdatetab = QtGui.QWidget()
self.softwareupdatetab.setObjectName(_fromUtf8("softwareupdatetab"))
self.label_7 = QtGui.QLabel(self.softwareupdatetab)
self.label_7.setGeometry(QtCore.QRect(50, 40, 311, 41))
font = QtGui.QFont()
font.setPointSize(20)
self.label_7.setFont(font)
self.label_7.setObjectName(_fromUtf8("label_7"))
self.label_8 = QtGui.QLabel(self.softwareupdatetab)
self.label_8.setGeometry(QtCore.QRect(370, 40, 121, 41))
font = QtGui.QFont()
font.setPointSize(20)
self.label_8.setFont(font)
self.label_8.setObjectName(_fromUtf8("label_8"))
self.groupBox_7 = QtGui.QGroupBox(self.softwareupdatetab)
self.groupBox_7.setGeometry(QtCore.QRect(10, 9, 461, 261))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.groupBox_7.setFont(font)
self.groupBox_7.setObjectName(_fromUtf8("groupBox_7"))
self.checkforupdatebutton = QtGui.QPushButton(self.groupBox_7)
self.checkforupdatebutton.setGeometry(QtCore.QRect(110, 90, 231, 91))
font = QtGui.QFont()
font.setPointSize(16)
self.checkforupdatebutton.setFont(font)
self.checkforupdatebutton.setObjectName(_fromUtf8("checkforupdatebutton"))
self.tabWidget.addTab(self.softwareupdatetab, _fromUtf8(""))
self.devmenutab = QtGui.QWidget()
self.devmenutab.setObjectName(_fromUtf8("devmenutab"))
self.groupBox_8 = QtGui.QGroupBox(self.devmenutab)
self.groupBox_8.setGeometry(QtCore.QRect(9, 10, 461, 261))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.groupBox_8.setFont(font)
self.groupBox_8.setObjectName(_fromUtf8("groupBox_8"))
self.closemenubutton = QtGui.QPushButton(self.groupBox_8)
self.closemenubutton.setGeometry(QtCore.QRect(20, 40, 191, 61))
font = QtGui.QFont()
font.setPointSize(16)
self.closemenubutton.setFont(font)
self.closemenubutton.setObjectName(_fromUtf8("closemenubutton"))
self.iteratenewpunch = QtGui.QPushButton(self.groupBox_8)
self.iteratenewpunch.setGeometry(QtCore.QRect(20, 180, 421, 61))
font = QtGui.QFont()
font.setPointSize(16)
self.iteratenewpunch.setFont(font)
self.iteratenewpunch.setObjectName(_fromUtf8("iteratenewpunch"))
self.cloneexistingpunchbutton = QtGui.QPushButton(self.groupBox_8)
self.cloneexistingpunchbutton.setGeometry(QtCore.QRect(20, 110, 421, 61))
font = QtGui.QFont()
font.setPointSize(16)
self.cloneexistingpunchbutton.setFont(font)
self.cloneexistingpunchbutton.setObjectName(_fromUtf8("cloneexistingpunchbutton"))
self.restartmenubutton = QtGui.QPushButton(self.groupBox_8)
self.restartmenubutton.setGeometry(QtCore.QRect(250, 40, 191, 61))
font = QtGui.QFont()
font.setPointSize(16)
self.restartmenubutton.setFont(font)
self.restartmenubutton.setObjectName(_fromUtf8("restartmenubutton"))
self.tabWidget.addTab(self.devmenutab, _fromUtf8(""))
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setSizeGripEnabled(False)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(9)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Punch Menu", None))
self.label_2.setText(_translate("MainWindow", "Beta 2", None))
self.flipscreenbutton.setText(_translate("MainWindow", "Flip Screen", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.hometab), _translate("MainWindow", "Home", None))
self.groupBox_5.setTitle(_translate("MainWindow", "Select Action", None))
self.restartbutton.setText(_translate("MainWindow", "Restart", None))
self.shutdownbutton.setText(_translate("MainWindow", "Shutdown", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.powertab), _translate("MainWindow", "Power", None))
self.groupBox.setTitle(_translate("MainWindow", "Select Connection Type", None))
self.dhcpbutton.setText(_translate("MainWindow", "DHCP (Auto)", None))
self.staticbutton.setText(_translate("MainWindow", "Static (Manual)", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.lanipconfigtab), _translate("MainWindow", "LAN IP Config", None))
self.groupBox_9.setTitle(_translate("MainWindow", "Select Action", None))
self.scanforaccesspointsbutton.setText(_translate("MainWindow", "Scan for Access Points", None))
self.connectwifibutton.setText(_translate("MainWindow", "Connect Wifi", None))
self.disconnectwifibutton.setText(_translate("MainWindow", "Disconnect Wifi", None))
self.erasestoredwifinetworksbutton.setText(_translate("MainWindow", "Erase Stored Wifi Networks", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.wificonfigtab), _translate("MainWindow", "Wifi IP Config", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.captivportallogintab), _translate("MainWindow", "Captive Portal Login", None))
self.groupBox_4.setTitle(_translate("MainWindow", "Feature Not Yet Implemented", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.wifiapconfigtab), _translate("MainWindow", "Wifi A/P Config", None))
self.groupBox_6.setTitle(_translate("MainWindow", "Select Action", None))
self.disconnectvpnbutton.setText(_translate("MainWindow", "Disconnect VPN", None))
self.connectvpnbutton.setText(_translate("MainWindow", "Connect VPN", None))
self.switchtotcpbutton.setText(_translate("MainWindow", "Switch to TCP", None))
self.switchtoudpbutton.setText(_translate("MainWindow", "Switch to UDP", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.vpnconfigtab), _translate("MainWindow", "VPN Config", None))
self.lanipbox.setTitle(_translate("MainWindow", "LAN IP", None))
self.lanipshow.setText(_translate("MainWindow", "IP GOES HERE", None))
self.groupBox_2.setTitle(_translate("MainWindow", "WIFI IP", None))
self.wifiipshow.setText(_translate("MainWindow", "IP GOES HERE", None))
self.groupBox_3.setTitle(_translate("MainWindow", "VPN IP", None))
self.vpnipshow.setText(_translate("MainWindow", "IP GOES HERE", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.statusdisplaytab), _translate("MainWindow", "Status Display", None))
self.label_7.setText(_translate("MainWindow", "Current Software Version:", None))
self.label_8.setText(_translate("MainWindow", "016", None))
self.groupBox_7.setTitle(_translate("MainWindow", "Software Version", None))
self.checkforupdatebutton.setText(_translate("MainWindow", "Check For Update", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.softwareupdatetab), _translate("MainWindow", "Software Update", None))
self.groupBox_8.setTitle(_translate("MainWindow", "Select Action", None))
self.closemenubutton.setText(_translate("MainWindow", "Close Menu", None))
self.iteratenewpunch.setText(_translate("MainWindow", "Iterate and Deploy New Punch!", None))
self.cloneexistingpunchbutton.setText(_translate("MainWindow", "Clone Existing Punch!", None))
self.restartmenubutton.setText(_translate("MainWindow", "Restart Menu", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.devmenutab), _translate("MainWindow", "Dev Menu", None))
from PyQt4 import QtWebKit
Does this help nail-down what isn't doing what it should be?
Your closemenu function needs to be part of your class.
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.closemenubutton.clicked.connect(self.closemenu)
# Notice this has been brought into the class
def closemenu(self):
sys.exit()
You didn't provide your UI file, so I can't perform an exact test, but I can show you that this works with a simple example:
This code creates a single button:
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.closebutton = QtGui.QPushButton('Exit', self)
self.closebutton.clicked.connect(self.exit_button)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.closebutton)
def exit_button(self):
sys.exit()
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
It looks like this:
When the button is clicked, the application closes with an exit status of 0 (a clean exit)
C:\Sandbox>python exit_button.py
C:\Sandbox>echo %ERRORLEVEL%
0
Whatever the underlying problem was, I was able to remedy it by switching to using Eric IDE in conjunction with Qt designer and following this tutorial: http://eric-ide.python-projects.org/tutorials/MiniBrowser/index.html to learn about the benefits of a development "environment" over what amounts to basically notepad that I was using before!
Defining actions is so much easier when you can see the handles you're attaching them too!
Other noobs reading this - follow that guide, it got me off the ground with object orientated programming and GUIs.

Categories

Resources