PyQt:将按钮链接到我的程序中的功能 [英] PyQt : Linking buttons to functions in my program

查看:24
本文介绍了PyQt:将按钮链接到我的程序中的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以假设我有这个 ui,它有 2 个要填充的空白文本点和一个运行"按钮.

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

我想让 2 个空文本点转到程序中的某些值,运行按钮基本上将运行 python 'Main' 程序......

I want to make it that the 2 empty text spots go to some values in the program and the run button will basically run the python 'Main' program ...

我该怎么做?

推荐答案

在 PyQt5 中,QWidgets 模块提供了一组 UI 元素来创建经典的桌面风格的用户界面.小部件可以显示数据和状态信息,接收用户输入,并为应该组合在一起的其他小部件提供容器.未嵌入父窗口小部件的窗口小部件称为窗口.父窗口小部件包含各种子窗口小部件.因此,首先您开始为窗口编写代码

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()

(QWidget 类是所有用户界面对象的基类).创建窗口后,您需要为 UI 窗口设置布局.Qt 中有许多布局类,但最常见的是 QVBoxLayout(垂直排列小部件.)和 QHBoxLayout(水平排列小部件.)并且很多时候它们两者都用于制作自定义布局.现在创建你的 QVBoxLayout

(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()

(注意这里 vbox 只是一个变量名).接下来是将小部件放置在窗口内,这可以作为

(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()

请注意,在 QPushButton 中,我们可以将按钮的名称作为其参数(在本例中为 ex-run).现在是事件和信号的时候了.要将 PushButton 连接到函数,我们编写 btn.clicked.connect(function_name) 这里 btn 是我们的 PushButton.注意这里 function_name 没有括号,这意味着我们没有调用函数,只是将按钮连接到函数上(当用户点击按钮时函数被执行).形式上可以写成

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)))

现在在我们的 main 函数中,我们首先从 text_1text_2 收集数据(有一个 text()QLineEdit 的 code> 方法从 QLineEdit 作为 str 获取数据.所以我们的 main 函数采用两个 text_1 的值text_2 并添加它们(如果输入的值无法转换为整数,则会引发错误)并通过 将该值设置为 text_3setText() 方法.

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.

现在你必须在我们之前创建的 vbox 中打包小部件

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)

并将窗口显示为

window.show()

现在缺少一件事,那就是这条线

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

app=QtWidgets.QApplication(sys.argv)

这一行是必要的,因为每个 PyQt5 应用程序都必须创建一个应用程序对象.sys.argv 参数是来自命令行的参数列表.现在我们必须创建应用程序的主循环.事件处理从这一点开始.app.exec_() 方法运行我们的应用程序,然后提供一个干净的退出.

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.

现在放在一起:

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

这将创建一个像这样的 UI 窗口:

This will make a UI window like this :

希望有帮助.如有问题,请发表评论.快乐编码!

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

这篇关于PyQt:将按钮链接到我的程序中的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆