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