Close Window and open a new one in Python with PyQt - python

I'm new to PyQt5 and want to code a small programm
I designed two Windows in QT Designer and convertet them into a py Script.
Afterwards i merged both into one file (maybe my attempt is totaly wrong - so pls correct me)
When the program starts I get a small Window with some Radio Buttons and OK and Cancel.
I want to start another Window by clicking on OK. (This works fine)
My problem is, that the initial Window stays active in Background. (How can I close only the initial window? all my attempts have closed the whole programm. I do not know what I have to change?
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class CLASS_WELCOME(object):
def setupUi(self, Welcome):
Welcome.setObjectName("Welcome")
Welcome.resize(204, 215)
Welcome.setWindowTitle("Welcome")
Welcome.setWhatsThis("")
self.buttonBox = QtWidgets.QDialogButtonBox(Welcome)
self.buttonBox.setGeometry(QtCore.QRect(0, 180, 191, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.frame = QtWidgets.QFrame(Welcome)
self.frame.setGeometry(QtCore.QRect(20, 40, 151, 131))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.radioButton = QtWidgets.QRadioButton(self.frame)
self.radioButton.setGeometry(QtCore.QRect(10, 10, 116, 22))
self.radioButton.setObjectName("radioButton")
self.radioButton_2 = QtWidgets.QRadioButton(self.frame)
self.radioButton_2.setGeometry(QtCore.QRect(10, 40, 116, 22))
self.radioButton_2.setObjectName("radioButton_2")
self.radioButton_3 = QtWidgets.QRadioButton(self.frame)
self.radioButton_3.setGeometry(QtCore.QRect(10, 70, 116, 22))
self.radioButton_3.setObjectName("radioButton_3")
self.radioButton_4 = QtWidgets.QRadioButton(self.frame)
self.radioButton_4.setGeometry(QtCore.QRect(10, 100, 116, 22))
self.radioButton_4.setObjectName("radioButton_4")
self.label = QtWidgets.QLabel(Welcome)
self.label.setGeometry(QtCore.QRect(10, 10, 191, 17))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.retranslateUi(Welcome)
self.buttonBox.accepted.connect(self.onOK)
self.buttonBox.rejected.connect(Welcome.reject)
QtCore.QMetaObject.connectSlotsByName(Welcome)
self.Gui = CLASS_GUI()
def retranslateUi(self, Welcome):
_translate = QtCore.QCoreApplication.translate
self.radioButton.setText(_translate("Welcome", "blue"))
self.radioButton_2.setText(_translate("Welcome", "green"))
self.radioButton_3.setText(_translate("Welcome", "red"))
self.radioButton_4.setText(_translate("Welcome", "yellow"))
self.label.setText(_translate("Welcome", "Mensch aergere dich nicht!"))
def onOK(self):
MenschUi = QtWidgets.QDialog()
MenschUi.ui = CLASS_GUI()
MenschUi.ui.setupUi(MenschUi)
MenschUi.exec_()
class CLASS_GUI(object):
def setupUi(self, MenschUi):
MenschUi.setObjectName("MenschUi")
MenschUi.resize(640, 485)
self.Abbrechen = QtWidgets.QDialogButtonBox(MenschUi)
self.Abbrechen.setGeometry(QtCore.QRect(290, 450, 341, 32))
self.Abbrechen.setOrientation(QtCore.Qt.Horizontal)
self.Abbrechen.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel)
self.Abbrechen.setCenterButtons(False)
self.Abbrechen.setObjectName("Abbrechen")
self.frame = QtWidgets.QFrame(MenschUi)
self.frame.setGeometry(QtCore.QRect(100, 10, 531, 431))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.Spieler = QtWidgets.QLabel(MenschUi)
self.Spieler.setGeometry(QtCore.QRect(10, 20, 66, 17))
self.Spieler.setObjectName("Spieler")
self.Computer = QtWidgets.QLabel(MenschUi)
self.Computer.setGeometry(QtCore.QRect(10, 100, 66, 17))
self.Computer.setObjectName("Computer")
self.LCDSpieler = QtWidgets.QLCDNumber(MenschUi)
self.LCDSpieler.setGeometry(QtCore.QRect(20, 40, 21, 23))
self.LCDSpieler.setDigitCount(1)
self.LCDSpieler.setObjectName("LCDSpieler")
self.LCDComputer = QtWidgets.QLCDNumber(MenschUi)
self.LCDComputer.setGeometry(QtCore.QRect(20, 130, 21, 23))
self.LCDComputer.setDigitCount(1)
self.LCDComputer.setObjectName("LCDComputer")
self.wuerfeln = QtWidgets.QPushButton(MenschUi)
self.wuerfeln.setGeometry(QtCore.QRect(10, 70, 81, 27))
self.wuerfeln.setObjectName("wuerfeln")
self.label = QtWidgets.QLabel(MenschUi)
self.label.setGeometry(QtCore.QRect(10, 450, 491, 21))
self.label.setText("")
self.label.setObjectName("label")
self.retranslateUi(MenschUi)
self.Abbrechen.accepted.connect(MenschUi.accept)
self.Abbrechen.rejected.connect(MenschUi.reject)
self.wuerfeln.clicked.connect(MenschUi.open)
QtCore.QMetaObject.connectSlotsByName(MenschUi)
def retranslateUi(self, MenschUi):
_translate = QtCore.QCoreApplication.translate
MenschUi.setWindowTitle(_translate("MenschUi", "Mensch Aergere dich Nicht!"))
self.Spieler.setText(_translate("MenschUi", "Spieler"))
self.Computer.setText(_translate("MenschUi", "Computer"))
self.wuerfeln.setText(_translate("MenschUi", "wuerfeln"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Welcome = QtWidgets.QDialog()
ui = CLASS_WELCOME()
ui.setupUi(Welcome)
Welcome.show()
sys.exit(app.exec_())

Possibly calling self.hide() in the beginning of the method onOK would do the job. (Untested)
You could lookup some Methods here.

Related

issue with QScrollArea

I am trying to add some check boxes in scroll area.but for me it is not working properly.i am new in python could anyone help?
import PyQt5
from PyQt5 import QtCore, QtGui, QtWidgets
from configparser import ConfigParser
import paramiko
import threading
import os
if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
class Ui_MainWindow(object):
def __init__(self, parent=None):
self.configur = ConfigParser()
self.configur.read('ServerConfigurationDetails.ini')
self.serverName=self.configur.sections()
print(self.serverName)
self.getComponents = self.configur.options("Component")
self.sLen = len(self.serverName)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(951, 634)
font = QtGui.QFont()
font.setPointSize(10)
MainWindow.setFont(font)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(150, 220, 91, 31))
font = QtGui.QFont()
font.setPointSize(14)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(250, 220, 231, 31))
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(700, 40, 101, 31))
self.pushButton.setObjectName("pushButton")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(140, 260, 681, 81))
self.groupBox.setObjectName("groupBox")
self.radioButton = QtWidgets.QRadioButton(self.groupBox)
self.radioButton.setGeometry(QtCore.QRect(10, 30, 201, 20))
self.radioButton.setObjectName("radioButton")
self.radioButton_2 = QtWidgets.QRadioButton(self.groupBox)
self.radioButton_2.setGeometry(QtCore.QRect(220, 30, 201, 20))
self.radioButton_2.setObjectName("radioButton_2")
self.checkBox = QtWidgets.QCheckBox(self.groupBox)
self.checkBox.setEnabled(False)
self.checkBox.setGeometry(QtCore.QRect(10, 50, 231, 31))
self.checkBox.setChecked(True)
self.checkBox.setObjectName("checkBox")
self.radioButton_3 = QtWidgets.QRadioButton(self.groupBox)
self.radioButton_3.setGeometry(QtCore.QRect(420, 30, 171, 20))
self.radioButton_3.setObjectName("radioButton_3")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(280, 40, 411, 31))
self.lineEdit.setObjectName("lineEdit")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(130, 40, 131, 31))
self.label_3.setObjectName("label_3")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(140, 125, 661, 21))
self.label_5.setText("")
self.label_5.setObjectName("label_5")
self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setGeometry(QtCore.QRect(130, 110, 681, 101))
self.scrollArea.setAccessibleDescription("")
self.scrollArea.setWidgetResizable(False)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 679, 99))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
c=10
n=0
x = 10
y = 10
print(self.sLen)
for i in range(self.sLen):
if(n==4):
n=0
x=10
y=y+20
self.checkbox1 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
self.checkbox1.setGeometry(QtCore.QRect(x+1, y+1, 161, 20))
self.checkbox1.setText(self.serverName[i])
self.checkbox1.setObjectName(self.serverName[i])
print(x)
print(n)
n=n+1
if(n==4):
x=10
y = y+20
else:
x= x+180
## self.checkBox_2 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
## self.checkBox_2.setGeometry(QtCore.QRect(10, 10, 161, 20))
## self.checkBox_2.setObjectName("checkBox_2")
## self.checkBox_3 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
## self.checkBox_3.setGeometry(QtCore.QRect(180, 10, 131, 20))
## self.checkBox_3.setObjectName("checkBox_3")
## self.checkBox_4 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
## self.checkBox_4.setGeometry(QtCore.QRect(350, 10, 151, 20))
## self.checkBox_4.setObjectName("checkBox_4")
## self.checkBox_5 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
## self.checkBox_5.setGeometry(QtCore.QRect(530, 10, 141, 20))
## self.checkBox_5.setObjectName("checkBox_5")
## self.checkBox_6 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
## self.checkBox_6.setGeometry(QtCore.QRect(10, 30, 161, 20))
## self.checkBox_6.setObjectName("checkBox_6")
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.scrollArea_2 = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea_2.setGeometry(QtCore.QRect(140, 380, 681, 191))
self.scrollArea_2.setWidgetResizable(True)
self.scrollArea_2.setObjectName("scrollArea_2")
self.scrollAreaWidgetContents_2 = QtWidgets.QWidget()
self.scrollAreaWidgetContents_2.setGeometry(QtCore.QRect(0, 0, 679, 189))
self.scrollAreaWidgetContents_2.setObjectName("scrollAreaWidgetContents_2")
self.scrollArea_2.setWidget(self.scrollAreaWidgetContents_2)
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setGeometry(QtCore.QRect(70, 340, 811, 20))
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 951, 26))
self.menubar.setObjectName("menubar")
self.menuhelp = QtWidgets.QMenu(self.menubar)
self.menuhelp.setObjectName("menuhelp")
MainWindow.setMenuBar(self.menubar)
self.actionAbout = QtWidgets.QAction(MainWindow)
self.actionAbout.setObjectName("actionAbout")
self.menuhelp.addAction(self.actionAbout)
self.menubar.addAction(self.menuhelp.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_2.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:10pt;\">Component</span></p></body></html>"))
self.comboBox_2.setItemText(0, _translate("MainWindow", "eComm"))
self.comboBox_2.setItemText(1, _translate("MainWindow", "workproc"))
self.pushButton.setText(_translate("MainWindow", "Search"))
self.groupBox.setTitle(_translate("MainWindow", "Other Details"))
self.radioButton.setText(_translate("MainWindow", "Only Search in Archive"))
self.radioButton_2.setText(_translate("MainWindow", "Also Search in Archive"))
self.checkBox.setText(_translate("MainWindow", "Auto Download"))
self.radioButton_3.setText(_translate("MainWindow", "Only Search in Log"))
self.label_3.setText(_translate("MainWindow", "<html><head/><body><p>Search String<span style=\" color:#ff0000;\">*</span></p></body></html>"))
## self.checkBox_2.setText(_translate("MainWindow", "CheckBox"))
## self.checkBox_3.setText(_translate("MainWindow", "CheckBox"))
## self.checkBox_4.setText(_translate("MainWindow", "CheckBox"))
## self.checkBox_5.setText(_translate("MainWindow", "CheckBox"))
## self.checkBox_6.setText(_translate("MainWindow", "CheckBox"))
self.menuhelp.setTitle(_translate("MainWindow", "Help"))
self.actionAbout.setText(_translate("MainWindow", "About"))
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_())
as you can see the scroll area is not showing properly.i have some 20 servers to show.this server list is coming from ini file and i need to show the check boxes in that scroll area.
you need to use QLayout to arrange you widgets. check out
Qt Designer -> "Widget Box" docker -> "Layouts" section
For the scrollarea part:
create scrollarea scroll_area = QScrollArea()
create view widget you want to put inside scrollarea base_widget = QWidget()
install view layout for view widget base_layout = QVBoxLayout(base_widget)
install view widget into scrollarea scroll_area.setWidget(base_widget)
fill in objects with view layout base_layout.addWidget(QLabel('foo')) base_layout.addWidget(QPushButton('bar')) base_layout.addLayout(my_sub_layout) etc.

How to switch widgets in stacked widget using a button?

So I have created a stacked widget with 2 pages. In the first page, I have one button " Sign In ". In the second page, I have One button " Logout"
I want that, if I click on the "Sign in" button it will open another page/widget with the " Logout " button in the stacked widget.
First Page Index = 0
Second page index = 1
Sign in button object name = pushButton
Logout button object name = LogoutBtn
this is my code from PyQt5 :
class Ui_StackedWidget(object):
def setupUi(self, StackedWidget):
StackedWidget.setObjectName("StackedWidget")
StackedWidget.resize(470, 406)
self.page1 = QtWidgets.QWidget()
self.page1.setObjectName("page1")
self.label = QtWidgets.QLabel(self.page1)
self.label.setGeometry(QtCore.QRect(150, 170, 35, 10))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.page1)
self.label_2.setGeometry(QtCore.QRect(150, 200, 35, 10))
self.label_2.setObjectName("label_2")
self.pushButton = QtWidgets.QPushButton(self.page1)
self.pushButton.setGeometry(QtCore.QRect(230, 230, 56, 17))
self.pushButton.setObjectName("pushButton")
self.lineEdit = QtWidgets.QLineEdit(self.page1)
self.lineEdit.setGeometry(QtCore.QRect(200, 170, 113, 16))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.page1)
self.lineEdit_2.setGeometry(QtCore.QRect(200, 200, 113, 16))
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_3 = QtWidgets.QLabel(self.page1)
self.label_3.setGeometry(QtCore.QRect(230, 120, 35, 10))
self.label_3.setObjectName("label_3")
StackedWidget.addWidget(self.page1)
self.page = QtWidgets.QWidget()
self.page.setObjectName("page")
self.label_4 = QtWidgets.QLabel(self.page)
self.label_4.setGeometry(QtCore.QRect(180, 70, 101, 16))
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.page)
self.label_5.setGeometry(QtCore.QRect(190, 110, 91, 16))
self.label_5.setObjectName("label_5")
self.CreateBtn = QtWidgets.QPushButton(self.page)
self.CreateBtn.setGeometry(QtCore.QRect(200, 160, 56, 17))
self.CreateBtn.setObjectName("CreateBtn")
self.ShowBtn = QtWidgets.QPushButton(self.page)
self.ShowBtn.setGeometry(QtCore.QRect(200, 190, 56, 17))
self.ShowBtn.setObjectName("ShowBtn")
self.DeleteBtn = QtWidgets.QPushButton(self.page)
self.DeleteBtn.setGeometry(QtCore.QRect(200, 220, 56, 17))
self.DeleteBtn.setObjectName("DeleteBtn")
self.UpdateBtn = QtWidgets.QPushButton(self.page)
self.UpdateBtn.setGeometry(QtCore.QRect(200, 250, 56, 17))
self.UpdateBtn.setObjectName("UpdateBtn")
self.LogoutBtn = QtWidgets.QPushButton(self.page)
self.LogoutBtn.setGeometry(QtCore.QRect(320, 320, 56, 17))
self.LogoutBtn.setObjectName("LogoutBtn")
StackedWidget.addWidget(self.page)
self.page_2 = QtWidgets.QWidget()
self.page_2.setObjectName("page_2")
self.label_6 = QtWidgets.QLabel(self.page_2)
self.label_6.setGeometry(QtCore.QRect(150, 90, 141, 16))
self.label_6.setObjectName("label_6")
self.Logout1Btn = QtWidgets.QPushButton(self.page_2)
self.Logout1Btn.setGeometry(QtCore.QRect(330, 320, 56, 17))
self.Logout1Btn.setObjectName("Logout1Btn")
self.main1Btn = QtWidgets.QPushButton(self.page_2)
self.main1Btn.setGeometry(QtCore.QRect(260, 320, 56, 17))
self.main1Btn.setObjectName("main1Btn")
self.label_7 = QtWidgets.QLabel(self.page_2)
self.label_7.setGeometry(QtCore.QRect(200, 180, 35, 10))
self.label_7.setObjectName("label_7")
StackedWidget.addWidget(self.page_2)
self.retranslateUi(StackedWidget)
QtCore.QMetaObject.connectSlotsByName(StackedWidget)
then I created a function switching ( i saw a reference in StackOverflow ) _
def switching(self):
self.ui = Ui_StackedWidget()
self.ui.setupUi(self)
self.ui.LogoutBtn.clicked.connect(lambda : self.ui.StackedWidget.setCurrentIndex(0))
self.ui.pushButton.clicked.connect(lambda : self.ui.StackedWidget.setCurrentIndex(1))
But it doesn't switch between windows.
According to the code you provide, I deduce that you used StackedWidget as a template, so the class you use fills in ui to inherit from that class.
An error that I see in your code is self.ui.StackedWidget since ui has no attribute called StackedWidget, so you should throw an exception.
Considering the above, the solution is:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_StackedWidget(object):
def setupUi(self, StackedWidget):
# ...
def retranslateUi(self, StackedWidget):
# ...
class StackedWidget(QtWidgets.QStackedWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_StackedWidget()
self.ui.setupUi(self)
self.ui.LogoutBtn.clicked.connect(lambda : self.setCurrentIndex(0))
self.ui.pushButton.clicked.connect(lambda : self.setCurrentIndex(1))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = StackedWidget()
w.show()
sys.exit(app.exec_())

Making Push button label change every click on pyqt5

I'm building a gui using pyqt5. I'm trying to figure out how to change a button every click:
in my code I have a push button called Chuck_Release. Basically, I want it to show either "chuck" or "release" under some conditions, but until then I just want it to change it's title every click, with the starting state of "chuck", but I can't seem to figure out how to do it.
This is my code:
import sys, time
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(685, 459)
self.exitbtn = QtWidgets.QPushButton(Dialog)
self.exitbtn.setGeometry(QtCore.QRect(608, 431, 75, 23))
self.exitbtn.setObjectName("exitbtn")
self.Typelist = QtWidgets.QComboBox(Dialog)
self.Typelist.setGeometry(QtCore.QRect(50, 70, 59, 20))
self.Typelist.setObjectName("Typelist")
self.Typelist.addItem("")
self.Typelist.addItem("")
self.Typelist.addItem("")
self.Chuck_Release = QtWidgets.QPushButton(Dialog)
self.Chuck_Release.setGeometry(QtCore.QRect(50, 100, 75, 23))
self.Chuck_Release.setObjectName("Chuck_Release")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(50, 20, 37, 16))
self.label.setObjectName("label")
self.textBrowser = QtWidgets.QTextBrowser(Dialog)
self.textBrowser.setGeometry(QtCore.QRect(25, 160, 511, 192))
self.textBrowser.setObjectName("textBrowser")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(50, 40, 133, 20))
self.lineEdit.setObjectName("lineEdit")
self.Runbtn = QtWidgets.QPushButton(Dialog)
self.Runbtn.setGeometry(QtCore.QRect(420, 60, 91, 61))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.Runbtn.setFont(font)
self.Runbtn.setObjectName("Runbtn")
self.getSerial = QtWidgets.QPushButton(Dialog)
self.getSerial.setGeometry(QtCore.QRect(200, 40, 75, 23))
self.getSerial.setObjectName("getSerial")
self.exitbtn.clicked.connect(self.exitclicked)
self.Chuck_Release.clicked.connect(self.chuckclicked)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "NEW GUI"))
self.exitbtn.setText(_translate("Dialog", "Exit"))
self.Typelist.setItemText(0, _translate("Dialog", "Type 1"))
self.Typelist.setItemText(1, _translate("Dialog", "Type2"))
self.Typelist.setItemText(2, _translate("Dialog", "Type 3"))
self.Chuck_Release.setText(_translate("Dialog", "Chuck"))
self.label.setText(_translate("Dialog", "Serial #"))
self.Runbtn.setText(_translate("Dialog", "Run"))
self.getSerial.setText(_translate("Dialog", "Enter"))
def exitclicked(self,Dialog):
sys.exit()
def chuckclicked(self,Dialog):
self.Chuck_Release.setText("Release") ***this is my function for chuckrelease push button***
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 would love if someone could explain me the process of how to get it done.
Thanks! :D
checkable : bool
This property holds whether the button is checkable
By default, the button is not checkable.
checked : bool
This property holds whether the button is checked
Only checkable buttons can be checked. By default, the button is unchecked.
import sys, time
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(685, 459)
self.exitbtn = QtWidgets.QPushButton(Dialog)
self.exitbtn.setGeometry(QtCore.QRect(608, 431, 75, 23))
self.exitbtn.setObjectName("exitbtn")
self.Typelist = QtWidgets.QComboBox(Dialog)
self.Typelist.setGeometry(QtCore.QRect(50, 70, 59, 20))
self.Typelist.setObjectName("Typelist")
self.Typelist.addItem("")
self.Typelist.addItem("")
self.Typelist.addItem("")
self.Chuck_Release = QtWidgets.QPushButton(Dialog)
self.Chuck_Release.setGeometry(QtCore.QRect(50, 100, 75, 23))
self.Chuck_Release.setObjectName("Chuck_Release")
self.Chuck_Release.setCheckable(True) # +++
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(50, 20, 37, 16))
self.label.setObjectName("label")
self.textBrowser = QtWidgets.QTextBrowser(Dialog)
self.textBrowser.setGeometry(QtCore.QRect(25, 160, 511, 192))
self.textBrowser.setObjectName("textBrowser")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(50, 40, 133, 20))
self.lineEdit.setObjectName("lineEdit")
self.Runbtn = QtWidgets.QPushButton(Dialog)
self.Runbtn.setGeometry(QtCore.QRect(420, 60, 91, 61))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.Runbtn.setFont(font)
self.Runbtn.setObjectName("Runbtn")
self.getSerial = QtWidgets.QPushButton(Dialog)
self.getSerial.setGeometry(QtCore.QRect(200, 40, 75, 23))
self.getSerial.setObjectName("getSerial")
self.exitbtn.clicked.connect(self.exitclicked)
self.Chuck_Release.clicked.connect(self.chuckclicked)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "NEW GUI"))
self.exitbtn.setText(_translate("Dialog", "Exit"))
self.Typelist.setItemText(0, _translate("Dialog", "Type 1"))
self.Typelist.setItemText(1, _translate("Dialog", "Type2"))
self.Typelist.setItemText(2, _translate("Dialog", "Type 3"))
self.Chuck_Release.setText(_translate("Dialog", "Chuck"))
self.label.setText(_translate("Dialog", "Serial #"))
self.Runbtn.setText(_translate("Dialog", "Run"))
self.getSerial.setText(_translate("Dialog", "Enter"))
def exitclicked(self,Dialog):
sys.exit()
def chuckclicked(self,Dialog):
# print(self.Chuck_Release.isChecked())
if self.Chuck_Release.isChecked(): # +++
self.Chuck_Release.setText("Release")
else:
self.Chuck_Release.setText("Chuck")
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_())

Implementing Timer to blink Q Push Buttons

Im trying to get some push buttons to blink from a QtDesigner file, but im having trouble using the timer function to change the background of the button.
I was already helped by someone here and their code works great, but I cant seem to integrate the code to what I already have.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(QtWidgets.QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1309, 813)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(50, 30, 251, 41))
font = QtGui.QFont()
font.setPointSize(36)
self.label.setFont(font)
self.label.setObjectName("label")
self.voltage_percent = QtWidgets.QLCDNumber(self.centralwidget)
self.voltage_percent.setGeometry(QtCore.QRect(80, 170, 141, 91))
self.voltage_percent.setSmallDecimalPoint(True)
self.voltage_percent.setDigitCount(1)
self.voltage_percent.setProperty("intValue", 5)
self.voltage_percent.setObjectName("voltage_percent")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(1070, 30, 111, 51))
font = QtGui.QFont()
font.setPointSize(36)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.modeTable = QtWidgets.QTableView(self.centralwidget)
self.modeTable.setGeometry(QtCore.QRect(970, 90, 321, 411))
self.modeTable.setObjectName("modeTable")
self.label_12 = QtWidgets.QLabel(self.centralwidget)
self.label_12.setGeometry(QtCore.QRect(170, 190, 51, 61))
font = QtGui.QFont()
font.setPointSize(48)
self.label_12.setFont(font)
self.label_12.setObjectName("label_12")
self.label_13 = QtWidgets.QLabel(self.centralwidget)
self.label_13.setGeometry(QtCore.QRect(580, 30, 91, 41))
font = QtGui.QFont()
font.setPointSize(36)
self.label_13.setFont(font)
self.label_13.setObjectName("label_13")
self.flags_table = QtWidgets.QGraphicsView(self.centralwidget)
self.flags_table.setGeometry(QtCore.QRect(370, 90, 541, 401))
self.flags_table.setObjectName("flags_table")
self.powerup_button = QtWidgets.QPushButton(self.centralwidget)
self.powerup_button.setGeometry(QtCore.QRect(380, 110, 111, 81))
self.powerup_button.setText("")
self.powerup_button.setObjectName("powerup_button")
self.flag = True
timer = QtCore.QTimer(self, interval=1000)
timer.timeout.connect(self.update)
timer.start(1000)
def update_color (self):
if self.flag==True:
self.powerup_button.setStyleSheet("background-color:none;")
else:
self.powerup_button.setStyleSheet("background-color: green;")
self.flag = False
I only included the part where I created my push button for simplicity, there are many more but I figured the same solution applies to all of them since they're the same. Am I just applying the timer wrong? the gui pops up, but the button I want to blink wont blink when I run the program (the powerup button)
Try it:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(QtWidgets.QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1309, 813)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(50, 30, 251, 41))
font = QtGui.QFont()
font.setPointSize(36)
self.label.setFont(font)
self.label.setObjectName("label")
self.voltage_percent = QtWidgets.QLCDNumber(self.centralwidget)
self.voltage_percent.setGeometry(QtCore.QRect(80, 170, 141, 91))
self.voltage_percent.setSmallDecimalPoint(True)
self.voltage_percent.setDigitCount(1)
self.voltage_percent.setProperty("intValue", 5)
self.voltage_percent.setObjectName("voltage_percent")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(1070, 30, 111, 51))
font = QtGui.QFont()
font.setPointSize(36)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.modeTable = QtWidgets.QTableView(self.centralwidget)
self.modeTable.setGeometry(QtCore.QRect(970, 90, 321, 411))
self.modeTable.setObjectName("modeTable")
self.label_12 = QtWidgets.QLabel(self.centralwidget)
self.label_12.setGeometry(QtCore.QRect(170, 190, 51, 61))
font = QtGui.QFont()
font.setPointSize(48)
self.label_12.setFont(font)
self.label_12.setObjectName("label_12")
self.label_13 = QtWidgets.QLabel(self.centralwidget)
self.label_13.setGeometry(QtCore.QRect(580, 30, 91, 41))
font = QtGui.QFont()
font.setPointSize(36)
self.label_13.setFont(font)
self.label_13.setObjectName("label_13")
self.flags_table = QtWidgets.QGraphicsView(self.centralwidget)
self.flags_table.setGeometry(QtCore.QRect(370, 90, 541, 401))
self.flags_table.setObjectName("flags_table")
self.powerup_button = QtWidgets.QPushButton(self.centralwidget)
self.powerup_button.setGeometry(QtCore.QRect(380, 110, 111, 81))
self.powerup_button.setText(" ")
self.powerup_button.setObjectName("powerup_button")
self.flag = True
timer = QtCore.QTimer(MainWindow) # +++
timer.timeout.connect(self.update_color)
timer.start(1000)
MainWindow.setCentralWidget(self.centralwidget)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def update_color (self):
if self.flag: #==True:
self.powerup_button.setStyleSheet("#powerup_button {background-color:none;}")
else:
self.powerup_button.setStyleSheet("#powerup_button {background-color: green;}")
# self.flag = False
self.flag = not self.flag # +
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Windows')
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

PyQt5 identical push buttons, one works one doesn't

I tried writing some codes using pyqt5 and have two main windows and push buttons, for both cases I have done same but one works on clicked.connect and one doesn't. Pasting code for the button which doesn't work. I call this window after closing first one
I tried creating module outside of class and that worked but then struggled with referencing to lineEdit objects
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
PREMIX = QtWidgets.QMainWindow()
logo = QtGui.QIcon()
def init_login():
ui = Ui_PREMIXlogin()
ui.setupUi(PREMIX)
PREMIX.show()
sys.exit(app.exec_())
def close_login():
PREMIX.hide()
def init_main():
ui = Ui_PREMIX()
ui.setupUi(PREMIX)
PREMIX.show()
class Ui_PREMIXlogin(object):
def setupUi(self, PREMIXlogin):
PREMIXlogin.setObjectName("PREMIXlogin")
PREMIXlogin.setEnabled(True)
PREMIXlogin.resize(280, 319)
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(
PREMIXlogin.sizePolicy().hasHeightForWidth()
)
PREMIXlogin.setSizePolicy(sizePolicy)
PREMIXlogin.setMinimumSize(QtCore.QSize(280, 319))
PREMIXlogin.setMaximumSize(QtCore.QSize(280, 319))
PREMIXlogin.setMouseTracking(False)
PREMIXlogin.setWindowIcon(logo)
PREMIXlogin.setAutoFillBackground(False)
PREMIXlogin.setStyleSheet("background-color: rgb(255, 255, 255);")
self.centralwidget = QtWidgets.QWidget(PREMIXlogin)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(36, 190, 61, 20))
font = QtGui.QFont()
font.setPointSize(10)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(36, 220, 61, 20))
font = QtGui.QFont()
font.setPointSize(10)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(110, 190, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(110, 220, 113, 20))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.lineEdit_2.setFont(font)
self.lineEdit_2.setInputMethodHints(
QtCore.Qt.ImhHiddenText
| QtCore.Qt.ImhNoAutoUppercase
| QtCore.Qt.ImhNoPredictiveText
| QtCore.Qt.ImhSensitiveData
)
self.lineEdit_2.setFrame(True)
self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
self.lineEdit_2.setObjectName("lineEdit_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(100, 260, 81, 31))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(False)
font.setWeight(50)
self.pushButton.setFont(font)
self.pushButton.setAutoFillBackground(False)
self.pushButton.setStyleSheet("background-color: rgb(222, 31, 38);")
self.pushButton.setObjectName("pushButton")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(30, 20, 231, 151))
self.frame.setStyleSheet(
"image: url(:/images/59301307_2353273704903079_3349139235981492224_n.png);"
)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
PREMIXlogin.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(PREMIXlogin)
self.statusbar.setObjectName("statusbar")
PREMIXlogin.setStatusBar(self.statusbar)
self.retranslateUi(PREMIXlogin)
QtCore.QMetaObject.connectSlotsByName(PREMIXlogin)
def retranslateUi(self, PREMIXlogin):
_translate = QtCore.QCoreApplication.translate
PREMIXlogin.setWindowTitle(_translate("PREMIXlogin", "PREMIX Login"))
self.label.setText(_translate("PREMIXlogin", "Username"))
self.label_2.setText(_translate("PREMIXlogin", "Password"))
self.pushButton.setText(_translate("PREMIXlogin", "Log in"))
self.pushButton.clicked.connect(self.on_click)
def on_click(self):
if True:
close_login()
init_main()
class Ui_PREMIX(object):
def setupUi(self, PREMIX):
PREMIX.setObjectName("PREMIX")
PREMIX.resize(473, 179)
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(PREMIX.sizePolicy().hasHeightForWidth())
PREMIX.setSizePolicy(sizePolicy)
PREMIX.setMinimumSize(QtCore.QSize(473, 179))
PREMIX.setMaximumSize(QtCore.QSize(473, 179))
PREMIX.setStyleSheet("background-color: rgb(255, 255, 255);")
PREMIX.setWindowIcon(logo)
self.centralwidget = QtWidgets.QWidget(PREMIX)
self.centralwidget.setObjectName("centralwidget")
self.Product = QtWidgets.QLabel(self.centralwidget)
self.Product.setGeometry(QtCore.QRect(30, 20, 61, 20))
font = QtGui.QFont()
font.setPointSize(13)
self.Product.setFont(font)
self.Product.setObjectName("Product")
self.Productname = QtWidgets.QLineEdit(self.centralwidget)
self.Productname.setGeometry(QtCore.QRect(100, 20, 251, 20))
self.Productname.setObjectName("Productname")
self.Checkbutton = QtWidgets.QPushButton(self.centralwidget)
self.Checkbutton.setGeometry(QtCore.QRect(370, 20, 81, 21))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(False)
font.setWeight(50)
self.Checkbutton.setFont(font)
self.Checkbutton.setAutoFillBackground(False)
self.Checkbutton.setStyleSheet("background-color: rgb(222, 31, 38);")
self.Checkbutton.setObjectName("Checkbutton")
self.logo = QtWidgets.QFrame(self.centralwidget)
self.logo.setGeometry(QtCore.QRect(359, 60, 101, 80))
self.logo.setStyleSheet(
"image: url(:/images/59301307_2353273704903079_3349139235981492224_n.png);"
)
self.logo.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.logo.setFrameShadow(QtWidgets.QFrame.Raised)
self.logo.setObjectName("logo")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(180, 80, 91, 31))
self.lineEdit.setMinimumSize(QtCore.QSize(89, 31))
self.lineEdit.setMaximumSize(QtCore.QSize(91, 31))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit.setReadOnly(True)
PREMIX.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(PREMIX)
self.statusbar.setObjectName("statusbar")
PREMIX.setStatusBar(self.statusbar)
self.retranslateUi(PREMIX)
QtCore.QMetaObject.connectSlotsByName(PREMIX)
def retranslateUi(self, PREMIX):
_translate = QtCore.QCoreApplication.translate
PREMIX.setWindowTitle(_translate("PREMIX", "PREMIX"))
self.Product.setText(_translate("PREMIX", "Product"))
self.Checkbutton.setText(_translate("PREMIX", "Check"))
self.Checkbutton.clicked.connect(self.on_click)
def on_click(self):
print("aaaaaa")
product = self.Productname.text()
print(product)
def main():
init_login()
if __name__ == "__main__":
main()
I want Checkbutton on click event to work, please help
For Ui_Premix, used self.Checkbutton.clicked.connect(Ui_PREMIX.on_click) instead of
self.on_click even though on_click event was inside class, it works now

Categories

Resources