Python Forum
PyQt5 Help - Absolute Beginner!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyQt5 Help - Absolute Beginner!
#1
Absolute beginner here, have no idea what I am doing wrong. All I want to do here is have the pushButton in PyQt5 to change to "Working..." and Red when clicked... which it currently does. Thing is I need it to also change back to the default "SCAN" and Green color when done running that method the button is linked to...

I know this is a super simple problem, and forgive any Novice errors in this code. I know very very little but if you guys can help me I would highly appreciate it!!! :)

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import pyautogui



class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.showMaximized()
        MainWindow.setMinimumSize(QtCore.QSize(0, 0))
        MainWindow.setMaximumSize(QtCore.QSize(3840, 2160))
        font = QtGui.QFont()
        font.setFamily("Arial Black")
        MainWindow.setFont(font)
        MainWindow.setStyleSheet("background-color: rgba(0, 85, 127, 100);")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(250, 250, 400, 150))
        font = QtGui.QFont()
        font.setFamily("Tahoma")
        font.setPointSize(24)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton.setFont(font)
        self.pushButton.setStyleSheet("background-color: rgb(0, 170, 0);\n"
"color: rgb(255, 255, 255);")
        self.pushButton.setObjectName("pushButton")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(730, 300, 701, 111))
        font = QtGui.QFont()
        font.setPointSize(18)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1920, 18))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setStyleSheet("background-color: rgba(0, 85, 127, 0);\n"
"color: rgb(255, 255, 255);")
        self.pushButton.setText(_translate("MainWindow", "SCAN"))
        self.label.setText(_translate("MainWindow", "WELCOME"))

        self.pushButton.clicked.connect(self.copy)

    def copy(self, MainWindow):
        self.pushButton.setText('WORKING...')
        self.pushButton.setStyleSheet("background-color: rgb(250, 0, 0);\n"
"color: rgb(255, 255, 255);")
        testprompt=storeid=pyautogui.prompt(text='test', title='test')
        


class Application():
    def run():
        import sys
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())

Application.run()
Reply
#2
I don't really know how to read PyQt5, but what it sounds like you need is a simple "if" statement. Something like this:
[python]
if working == True:
##put your color change to red and process here, then
##working = False
else:
#put your code for a green button and it waiting here.
#when it is clicked, change working to True
[python]

The specifics are up to you, but it's an if you are looking for.
Reply
#3
mnash you are doing way too much typing. I usually let the Qt pick my fonts. But if I really wanted a particular font I would use a style sheet file that I would load into the application instead of hard coding a font. If you really want to hard code fonts at least use a convenient constructor:
MainWindow.setFont(QFont("Times", 10, QFont.Bold))
And what's with the odd class methods? If you are writing window classes by hand, all the examples are written the same way. You define a class for each window. You make all the widgets inside the __init___ method. The script calls QApplicaiton, creates instances of the window classes, draws a window (or more) and calls app.exec_.
import stuff

class MyCustomWindow(QtWidgets.QMainWindow):  # <- A custom QMainWindow
    def __init__(self): # <- Called when instances is created
        super().__init__() # <- Do __init__ for QMainWindow
        self.setWindowTitle('Window Title')
        
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        # Do not set the geometry.  Let the window take care of this auromatically
        # self.menubar.setGeometry(QtCore.QRect(0, 0, 1920, 18))
        self.menubar.setObjectName("menubar")
        self.setMenuBar(self.menubar)
        
        self.layout = QVBoxLayout(self)  # <- Use a layout manager

        self.button = QPushButton() # <-Use default font
        self.set_button_style('SCAN', 'rgb(0, 250, 0)')
        self.button.connect(self.copy)
        self.layout.addWidget(self.button)

    def set_button_style(self, button, text, bg_color):
        self.pushButton.setText(text)
        stylesheet = f'background_color: {bg_color}'
        button.setText(text)
        button.setStyleSheet(stylesheet)

    def copy(self, MainWindow):
        self.set_button_style('WORKING...', 'rgb(250, 0, 0)')
        testprompt=storeid=pyautogui.prompt(text='test', title='test')
        self.set_button_style('SCAN', 'rgb(0, 250, 0)')

if __name__ == '__main__':
    app = QtWidgets.Application(sys.argv) # use -stylesheet file to set styles

    mainwindow = MyCustomWindow('Window Title')
    mainwindow.show()
    sys.exit(app.exec_())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,785 Apr-06-2019, 11:15 PM
Last Post: ZenWoR

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020