PyQt : Linking buttons to functions in my program

前端 未结 1 1815
生来不讨喜
生来不讨喜 2021-01-24 13:16

So lets say i have this ui that has 2 empty text spots to fill and a \'Run\' button.

I want to make it that the 2 empty text spots go to some values in the program and t

相关标签:
1条回答
  • 2021-01-24 13:48

    In PyQt5 the QWidgets module provides a set of UI elements to create classic desktop-style user interfaces. Widgets can display data and status information, receive user input, and provide a container for other widgets that should be grouped together. A widget that is not embedded in a parent widget is called a window.A parent widget containing various child widgets.So first you start to write a code for your window as

    window=QtWidgets.QWidget()
    

    (The QWidget class is the base class of all user interface objects).Once your window is created you need to set a layout for your UI window. There are many classes for layouts in Qt but most common are QVBoxLayout(to line up widgets vertically.) and QHBoxLayout(to line up widgets horizontally.) and many a times they both are used to make a custom layout. Now create your QVBoxLayout as

    vbox=QWidgets.QVBoxLayout()
    

    (note here that vbox is just a variable name).The next thing is to place widgets inside the window which can be done as

    text_1=QtWidgets.QLineEdit()
    text_2=QtWidgets.QLineEdit()
    run_btn=QtWidgets.QPushButton("run")
    text_3=QtWidgets.QLineEdit()
    

    Note that in QPushButton we can give the name of the button as its argument(ex-run in this case). Now is the time for events and signals. To connect the PushButton to a function , we write btn.clicked.connect(function_name) here btn is our PushButton.Note here that the function_name is without parenthesis which means that we have not called the function , just connected the button to the function (when the user will click the button the function gets executed).Foramlly this can be written as

    run_btn=QtWidgets.QPushButton("run")
    def main():
        data_1=text_1.text()
        data_2=text_2.text()
        text_3.setText(str(int(data_1)+int(data_2)))
    

    Now in our main function we first collected the data from text_1 and text_2 (there is a text() method for QLineEdit to get the data from QLineEdit as a str).So our main function takes the values of both text_1 and text_2 and adds them (it will raise an error if the values entered cannot be converted into integers) and sets that value to the text_3 by setText() method.

    Now you have to pack the widgets in our vbox which we created earlier as

    vbox.addWidget(text_1)
    vbox.addWidget(text_2)
    vbox.addWidget(run_btn)
    vbox.addWidget(text_3)
    

    And now set the layout of our window as

    window.setLayout(vbox)
    

    And to show the window as

    window.show()
    

    By now one thing is missing and that's the line

    app=QtWidgets.QApplication(sys.argv)
    

    This line is necessary as every PyQt5 application must create an application object. The sys.argv parameter is a list of arguments from a command line. Now we have to create the mainloop of the application. The event handling starts from this point. The app.exec_() method runs our application and then provide a clean exit.

    Now putting all together :

    import sys
    from PyQt5 import QtWidgets
    
    app=QtWidgets.QApplication(sys.argv)
    window=QtWidgets.QWidget()
    vbox=QtWidgets.QVBoxLayout()
    text_1=QtWidgets.QLineEdit()
    text_2=QtWidgets.QLineEdit()
    run_btn=QtWidgets.QPushButton("run")
    text_3=QtWidgets.QLineEdit()
    def main():
        data_1=text_1.text()
        data_2=text_2.text()
        text_3.setText(str(int(data_1)+int(data_2)))
    run_btn.clicked.connect(main)
    vbox.addWidget(text_1)
    vbox.addWidget(text_2)
    vbox.addWidget(run_btn)
    vbox.addWidget(text_3)
    window.setLayout(vbox)
    window.show()
    sys.exit(app.exec_())
    

    This will make a UI window like this :

    Hope it helps. Please do comment in case of some problem . Happy coding!

    0 讨论(0)
提交回复
热议问题