将 python 函数作为信号直接应用到 Qt 设计器

发布于 2024-12-23 07:25:10 字数 397 浏览 5 评论 0原文

总的来说,我对 Qt 和 GUI 编程很陌生,但我已经用 python 编写了相当多的代码 - 编写模块等等。我需要为我的一些旧模块开发简单的 GUI。

我想要做的事情可以用下面的简单示例来表示:

def f(x, y):
    z = x + y
    return z

对于这个函数,我将为 x 和 y 提供两行编辑,为 z 提供一行编辑。现在我创建一个按钮“计算”,当我这样做时,我希望它从行编辑中获取 x 和 y 运行函数 f(x,y) 并将输出提供给 z 。

有没有办法通过添加用 python 编写的函数 f(x,y) 直接在 Qt Designer 中执行此操作?

如果不是,还有什么替代方案?

I am new to Qt and GUI programming overall but i have done a fair bit of coding in python - writing modules and so on. I need to develop simple GUIs for some of my old modules.

What i am trying to do can be represented by the following simple example:

def f(x, y):
    z = x + y
    return z

For this function i will give two line-edits for x and y and one for z. Now i create a push-button 'calculate' and when i do that i want it to take x and y from the line-edits run the function f(x,y) and give the output to z.

Is there any way to do this directly in Qt Designer by adding the function f(x,y) written in python?

If not what are the alternatives?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我鬧 2024-12-30 07:25:10

编写 PyQt4 gui 时的基本工作流程是:

  1. 使用 Qt Designer 设计 UI。
  2. 使用 pyuic4 从 UI 文件生成 Python 模块。
  3. 为主程序逻辑创建一个应用程序模块。
  4. 将 GUI 类导入到应用程序模块中。
  5. 将 GUI 连接到程序逻辑。

因此,给定 UI 文件 calc.ui,您可以使用以下方式生成 UI 模块:

pyuic4 -w calc.ui > calc_ui.py

然后创建一个如下所示的应用程序模块:

from PyQt4 import QtGui, QtCore
from calc_ui import CalculatorUI

class Calculator(CalculatorUI):
    def __init__(self):
        CalculatorUI.__init__(self)
        self.buttonCalc.clicked.connect(self.handleCalculate)

    def handleCalculate(self):
        x = int(self.lineEditX.text())
        y = int(self.lineEditY.text())
        self.lineEditZ.setText(str(x + y))

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Calculator()
    window.show()
    sys.exit(app.exec_())

请注意,它有助于设置 objectName对于 Designer 的属性编辑器中的每个小部件,以便以后可以轻松识别它们。特别是,主窗体的 objectName 将成为导入的 GUI 类的类名(假设使用了 pyuic4 的“-w”标志)。

The basic workflow when writing a PyQt4 gui is:

  1. Design the UI using Qt Designer.
  2. Generate a Python module from the UI file using pyuic4.
  3. Create an Application module for the main program logic.
  4. Import the GUI class into the Application module.
  5. Connect the GUI to the program logic.

So, given the UI file calc.ui, you could generate the UI module with:

pyuic4 -w calc.ui > calc_ui.py

And then create an application module something like this:

from PyQt4 import QtGui, QtCore
from calc_ui import CalculatorUI

class Calculator(CalculatorUI):
    def __init__(self):
        CalculatorUI.__init__(self)
        self.buttonCalc.clicked.connect(self.handleCalculate)

    def handleCalculate(self):
        x = int(self.lineEditX.text())
        y = int(self.lineEditY.text())
        self.lineEditZ.setText(str(x + y))

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Calculator()
    window.show()
    sys.exit(app.exec_())

Note that it helps to set the objectName for every widget in Designer's Property Editor so that they can be easily identified later on. In particular, the objectName of the main form will become class-name of the GUI class that is imported (assuming the "-w" flag to pyuic4 is used).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文