Python Forum
PyQt5 Help - Absolute Beginner!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyQt5 Help - Absolute Beginner!
#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


Messages In This Thread
PyQt5 Help - Absolute Beginner! - by mnash48 - Mar-18-2020, 10:02 PM
RE: PyQt5 Help - Absolute Beginner! - by deanhystad - Mar-20-2020, 11:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,847 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