stay PyQt5 in ,QWidgets The module provides a set of UI Element to create a classic desktop user interface . Widgets can display data and status information , Receive user input , And provide containers for other widgets that should be grouped together . A widget that is not embedded in the parent control is called a window . Contains the parent widget for each child control . So first , You start writing a code for your window window=QtWidgets.QWidget()

(QWidget Class is the base class for all user interface objects ). After creating the window , Need for UI Window setup layout .Qt There are many classes for layout in , But the most common is QVBoxLayout( Arrange widgets vertically ) and {}( Arrange widgets horizontally ), Many times they are used to make custom layouts . Now your QVBoxLayout Create as

^{pr2}$

( Please note that ,vbox Just a variable name )text_1=QtWidgets.QLineEdit()

text_2=QtWidgets.QLineEdit()

run_btn=QtWidgets.QPushButton("run")

text_3=QtWidgets.QLineEdit()

be careful , stay QPushButton in , We can take the name of the button as its parameter ( In this case, yes ex-run). Now is the time for events and signals .

To connect a button to a function , Let's write down btn.clicked.connect(function_name) here btn It's our button . Note that the function name here has no parentheses , That means we didn't call the function , Just connect the button to the function ( When the user clicks the button , The function will be executed )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 In function , Let's start with text_1 and {} collecting data ( There is one text() method ,QLineEdit from QLineEdit get data , As str). therefore ,main Functions accept at the same time {} and {} And add them ( If the value entered cannot be converted to an integer , An error is raised ) And pass setText() Method to set the value to text_3. stay

Now you must package the widget into the one we created earlier vbox in vbox.addWidget(text_1)

vbox.addWidget(text_2)

vbox.addWidget(run_btn)

vbox.addWidget(text_3)

Now set the layout of the window to window.setLayout(vbox)

Show the window as window.show()

Up to now , One thing is missing , That's the line app=QtWidgets.QApplication(sys.argv)

This line is required , Because every PyQt5 Every application must create one application object .sys.argv Parameters are a list of parameters on the command line .

Now we must create the main loop of the application . Event handling starts at this point .app.exec_() Method to run our application , Then provide a clean exit . stay

Put all this together now :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 UI The window is shown below :

I hope it helps .

If you have any questions, please comment .

Coding happiness ! stay

Technology
©2019-2020 Toolsou All rights reserved,
【 Explain in detail 】 Interview must ask :SpringBoot Automatic configuration principle 【 Hyper detail 】Java Realization of student information management system 2020 The 11th National Blue Bridge Cup Python group JavaScript note ( five )---- Branch statement MYSQL database SQL Sentence practice experiment EXERCISES. 1 SIMPLE COMMANDSMySQL Basics Commonly used sentence ( Add / delete / modify query )C++ of string of compare usage vue limit input Only positive numbers can be entered C Language of a Gobang game implementation Programmer refused due to low salary offer,HR become shame , Netizens instantly blew up ..