Making Push button label change every click on pyqt5 - python

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

Related

pyqt app crashing when function is executed

i wanted to make an rpc changer with pypresence and pyqt and its working, but it crashes whenever i run function that updates the presence - no errors, no exit codes it says that app doesnt respond.
i tried to use qt threading but it didnt work i also tried making it just a normal function, not a loop and it didnt work either.
any ideas?
and heres the code:
from pypresence import Presence
import time
from PyQt5 import QtCore, QtGui, QtWidgets
start = int(time.time())
client_id = "997609359427260416"
RPC = Presence(client_id)
RPC.connect()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(485, 425)
MainWindow.setStyleSheet("background-color: #1f1f1f;""font: 75 8pt \"Arial\";")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget, clicked = lambda: self.onclick())
self.pushButton.setGeometry(QtCore.QRect(160, 350, 161, 51))
self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.pushButton.setAutoFillBackground(False)
self.pushButton.setStyleSheet("background-color: #a30b00;""color: white;""transition: 0.25s;""font: 12pt;""border-radius: 8px;""")
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(120, 10, 251, 31))
self.label.setStyleSheet("color: white;""font: 18pt")
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(210, 40, 91, 16))
self.label_2.setStyleSheet("color: #a30b00;""font: 12pt")
self.label_2.setObjectName("label_2")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(80, 100, 321, 31))
self.lineEdit.setStyleSheet("background-color: rgb(44, 44, 44);""border: 2px solid rgb(60, 60, 60);""border-radius: 5px;""color: white;")
self.lineEdit.setObjectName("lineEdit")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(80, 80, 121, 16))
self.label_3.setStyleSheet("color: white;""font-size: 12pt")
self.label_3.setObjectName("label_3")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(80, 170, 321, 31))
self.lineEdit_2.setStyleSheet("background-color: rgb(44, 44, 44);""border: 2px solid rgb(60, 60, 60);""border-radius: 5px;""color: white;")
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(80, 150, 121, 16))
self.label_4.setStyleSheet("color: white;""font-size: 12pt")
self.label_4.setObjectName("label_4")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(80, 230, 321, 31))
self.lineEdit_3.setStyleSheet("background-color: rgb(44, 44, 44);""border: 2px solid rgb(60, 60, 60);""border-radius: 5px;""color: white;")
self.lineEdit_3.setObjectName("lineEdit_3")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(80, 210, 121, 16))
self.label_5.setStyleSheet("color: white;""font-size: 12pt")
self.label_5.setObjectName("label_5")
self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_4.setGeometry(QtCore.QRect(80, 290, 321, 31))
self.lineEdit_4.setStyleSheet("background-color: rgb(44, 44, 44);""border: 2px solid rgb(60, 60, 60);""border-radius: 5px;""color: white;")
self.lineEdit_4.setObjectName("lineEdit_4")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(80, 270, 121, 16))
self.label_6.setStyleSheet("color: white;""font-size: 12pt")
self.label_6.setObjectName("label_6")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def onclick(self):
value1 = self.lineEdit.text()
while True:
RPC.update(
large_image = "large", #name of your asset
large_text = "test",
details = value1,
state = "Presence Changer",
start = start,
buttons = [{"label": "kndxiu on TikTok", "url": "https://tiktok.com/#kndxiu"}, {"label": "Website", "url": "https://kndxiu.xyz"}] #up to 2 buttons
)
time.sleep(100)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "SAVE"))
self.label.setText(_translate("MainWindow", "Discord RP Changer"))
self.label_2.setText(_translate("MainWindow", "by kndxiu."))
self.label_3.setText(_translate("MainWindow", "test"))
self.label_4.setText(_translate("MainWindow", "test"))
self.label_5.setText(_translate("MainWindow", "test"))
self.label_6.setText(_translate("MainWindow", "test"))
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_())
i found a solution, i used qtimer and replaced onclick function with this:
def onclick(self):
value1 = self.lineEdit.text()
def updateLoop():
RPC.update(
large_image = "large", #name of your asset
large_text = "test",
details = value1,
state = "Presence Changer",
start = start,
buttons = [{"label": "kndxiu on TikTok", "url": "https://tiktok.com/#kndxiu"}, {"label": "Website", "url": "https://kndxiu.xyz"}] #up to 2 buttons
)
self.timer = QTimer()
self.timer.timeout.connect(updateLoop)
self.timer.setInterval(100)
self.timer.start()
and ofc from PyQt5.QtCore import QTimer at the top and it works fine, without freezing

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.

PyQt: How to refresh label on one window with push button on another one?

Hello dear Users of stackoverflow
Introduction:
I am kind of new to Python and want to build a touch GUI for my Raspberry Pi with PyQt5. Therefore, I use the QtDesigner to build up .ui files on Windows 7. After that, the files are translated to .py files using "pyuic5 -x file.ui -o file.py" in the LXTerminal of the Pi.
My GUI:
I need to build up one output window (MainWindow) with a label and a push button, which opens up another window (I chose Dialog) for input. The input window has a spin box to set the value and a horizontal slider for bigger value steps. At the bottom of the window is a push button, which sets the spin box value as global variable and closes the Input window again.
The problem:
I want the push button of the input window that closes this window to also refresh the output label of the MainWindow, so that it shows the new value.
Pictures:
I am not allowed to embed Pictures of my GUI yet, so please see the following links.
MainWindow
InputWindow
InputWindow with Connections between slider and spin box
My Code:
The following code is a simple example and has everything working except the refresh of the Label. Please help me to get this work, even if it might be very simple for advanced and professional developers. I spent days on trying and googling for this and got lots of more complicated things working.
Best wishes,
RaspiManu
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
value = 0
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(890, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(180, 100, 500, 250))
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setStyleSheet("background-color: rgb(255, 255, 255);")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 370, 500, 100))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
############
self.pushButton.clicked.connect(self.OpenInput)
############
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", "Value"))
self.pushButton.setText(_translate("MainWindow", "Go to input window"))
##############################
# Show second window for input
def OpenInput(self, MainWindow):
Dialog.show()
##############################
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(889, 598)
self.spinBox = QtWidgets.QSpinBox(Dialog)
self.spinBox.setGeometry(QtCore.QRect(210, 170, 471, 141))
font = QtGui.QFont()
font.setPointSize(33)
self.spinBox.setFont(font)
self.spinBox.setAlignment(QtCore.Qt.AlignCenter)
self.spinBox.setObjectName("spinBox")
self.horizontalSlider = QtWidgets.QSlider(Dialog)
self.horizontalSlider.setGeometry(QtCore.QRect(209, 360, 471, 61))
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(310, 460, 271, 71))
font = QtGui.QFont()
font.setPointSize(13)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
############
self.pushButton.clicked.connect(self.CloseAndRefresh)
############
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(210, 40, 471, 91))
font = QtGui.QFont()
font.setPointSize(24)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.retranslateUi(Dialog)
self.horizontalSlider.valueChanged['int'].connect(self.spinBox.setValue)
self.spinBox.valueChanged['int'].connect(self.horizontalSlider.setValue)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "Back to first Window"))
self.label.setText(_translate("Dialog", "Value"))
#######################################################
# Close second window and refresh label on first window
def CloseAndRefresh(self):
global value
value = self.spinBox.value()
print(value) #checking input
##################################################
# The refresh of the outputting label on the #
# MainWindow should be started at this position. #
##################################################
Dialog.close()
#######################################################
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
Dialog = QtWidgets.QDialog()
dia = Ui_Dialog()
dia.setupUi(Dialog)
sys.exit(app.exec_())
Try it:
from PyQt5 import QtCore, QtGui, QtWidgets
value = 0
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(890, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(180, 100, 500, 250))
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setStyleSheet("background-color: rgb(255, 255, 255);")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 370, 500, 100))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
############
self.pushButton.clicked.connect(self.OpenInput)
############
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", "Value"))
self.pushButton.setText(_translate("MainWindow", "Go to input window"))
##############################
# Show second window for input
def OpenInput(self, MainWindow):
Dialog.show()
##############################
# +++
def labelText(self, MainWindow, value):
self.label.setText(str(value))
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(889, 598)
self.spinBox = QtWidgets.QSpinBox(Dialog)
self.spinBox.setGeometry(QtCore.QRect(210, 170, 471, 141))
font = QtGui.QFont()
font.setPointSize(33)
self.spinBox.setFont(font)
self.spinBox.setAlignment(QtCore.Qt.AlignCenter)
self.spinBox.setObjectName("spinBox")
self.horizontalSlider = QtWidgets.QSlider(Dialog)
self.horizontalSlider.setGeometry(QtCore.QRect(209, 360, 471, 61))
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(310, 460, 271, 71))
font = QtGui.QFont()
font.setPointSize(13)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
############
self.pushButton.clicked.connect(self.CloseAndRefresh)
############
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(210, 40, 471, 91))
font = QtGui.QFont()
font.setPointSize(24)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.retranslateUi(Dialog)
self.horizontalSlider.valueChanged['int'].connect(self.spinBox.setValue)
self.spinBox.valueChanged['int'].connect(self.horizontalSlider.setValue)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "Back to first Window"))
self.label.setText(_translate("Dialog", "Value"))
#######################################################
# Close second window and refresh label on first window
def CloseAndRefresh(self):
global value
value = self.spinBox.value()
print(value) #checking input
# +++
ui.labelText(MainWindow, value)
##################################################
# The refresh of the outputting label on the #
# MainWindow should be started at this position. #
##################################################
Dialog.close()
#######################################################
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
Dialog = QtWidgets.QDialog()
dia = Ui_Dialog()
dia.setupUi(Dialog)
sys.exit(app.exec_())

Basic GUI Weight converter

I am writing a Weight converter to convert killogram to Pounds and vice versa....
Using QT Desginer I have done the interface etc, but now I am stuck trying to figure out how to link the buttons so that when you press one it converts to pound what is entered and vice versa...
Coding so Far:
# 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(486, 406)
self.pushButton = QtWidgets.QPushButton(MainWindow)
self.pushButton.setGeometry(QtCore.QRect(160, 320, 151, 41))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(MainWindow)
self.pushButton_2.setGeometry(QtCore.QRect(160, 60, 151, 41))
self.pushButton_2.setObjectName("pushButton_2")
self.label = QtWidgets.QLabel(MainWindow)
self.label.setGeometry(QtCore.QRect(86, 20, 301, 31))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(MainWindow)
self.label_2.setGeometry(QtCore.QRect(20, 230, 131, 21))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(MainWindow)
self.label_3.setGeometry(QtCore.QRect(350, 230, 111, 21))
self.label_3.setObjectName("label_3")
self.lineEdit = QtWidgets.QLineEdit(MainWindow)
self.lineEdit.setGeometry(QtCore.QRect(20, 170, 113, 41))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(MainWindow)
self.lineEdit_2.setGeometry(QtCore.QRect(340, 170, 113, 41))
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_4 = QtWidgets.QLabel(MainWindow)
self.label_4.setGeometry(QtCore.QRect(10, 60, 441, 311))
self.label_4.setToolTipDuration(2)
self.label_4.setText("leh")
self.label_4.setPixmap(QtGui.QPixmap("../../../../../Desktop/unbalanced-
scale.png"))
self.label_4.setIndent(-4)
self.label_4.setObjectName("label_4")
self.label_4.raise_()
self.pushButton.raise_()
self.pushButton_2.raise_()
self.label.raise_()
self.label_2.raise_()
self.label_3.raise_()
self.lineEdit.raise_()
self.lineEdit_2.raise_()
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Main Window"))
self.pushButton.setText(_translate("MainWindow", "<<< Pounds to Killogram"))
self.pushButton_2.setText(_translate("MainWindow", "Killogram to Pounds >>>"))
self.label.setText(_translate("MainWindow", " Welcom to PJ Nothnagel\'s Weight Converter!"))
self.label_2.setText(_translate("MainWindow", " Killograms"))
self.label_3.setText(_translate("MainWindow", " Pounds"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QDialog()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

Close Window and open a new one in Python with PyQt

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.

Categories

Resources