读取 UI 文件并连接按钮、文本等元素 [英] reading UI file and connecting elements such as buttons, text, etc

查看:55
本文介绍了读取 UI 文件并连接按钮、文本等元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用 PySide2,但读取 UI 文件,而不是通过 pyside2-uic 生成 Python.奇怪的是,我似乎找不到这种简单连接的例子.

I'm looking at using PySide2, but reading UI files, instead of generating Python via pyside2-uic. Strangely, I can't seem to find an example of this simple connectivity.

我看到了 PyQt5 和 PySide2 之间的区别:

I see the difference between PyQt5 and PySide2:

https://www.learnpyqt.com/blog/pyqt5-vs-pyside2/

但不清楚按钮在使用 PySide2 时如何连接.

but am unclear on how a button would hook up in using PySide2.

最简单的弹出窗口的代码在这里;我不太清楚的是连接到在 UI 中创建的元素 (btnTest) 的位.我已经让这些东西与 Qt 一起使用了,但是语法让我望而却步.一旦弄清楚了,其余的应该随之而来.

The simplest code which brings up the window is here; what I can't quite figure is the bit that hooks up to the element (btnTest) which was created in the UI. I've gotten this stuff to work with Qt, but the syntax eludes me. Once that is figured out, the rest should follow.

import sys
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import (QToolTip, QMessageBox, QPushButton, \
QApplication, QCheckBox, QDialog, QFileDialog, QGraphicsScene, QWidget, \
QLabel,QMainWindow, QDialogButtonBox)

'''

button hookup is here

'''



if __name__ == '__main__':
    print("Program start.")
    loader = QUiLoader()
    app = QtWidgets.QApplication(sys.argv)
    window = loader.load("test.ui", None)
    window.show()
    app.exec_()

按钮的 XML(.ui 文件):

And the XML (.ui file) for the button:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>210</width>
    <height>117</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="btnTest">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>30</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>PushButton</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>210</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

我见过这样的结构,但不太确定我错过了什么;最初的答案让我继续前进,但我认为有一种更 Pythonic 的做事方式.需要明确的是 - 以下操作无效(给出的解决方案有效);我试图模仿如何从转换后的 .uic 文件中包含 .py 文件.

I've seen structures like this, but am not quite sure what I'm missing; the initial answer gets me going, but I think there is a more pythonic way of doing things. To be clear - the following does not work (the solution given does); I was trying to mimic how including the .py file from the converted .uic file was set up.

class Form(QMainWindow):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.btnTest.clicked.connect(ProcessClick)

    def ProcessClick(self):
        print("hi")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Form()
    loader = QUiLoader()
    window = loader.load("test.ui", None)
    window.show()
    app.exec_()

推荐答案

根据 XML 的按钮名称是 btnTest:

According to the XML the button name is btnTest:

<widget class="QPushButton" name="btnTest">

因此您必须使用该名称来访问按钮:

So you must use that name to access the button:

import sys

from PySide2 import QtWidgets, QtUiTools


if __name__ == "__main__":
    print("Program start.")

    app = QtWidgets.QApplication(sys.argv)
    loader = QtUiTools.QUiLoader()
    window = loader.load("test.ui", None)

    window.btnTest.clicked.connect(lambda: print("clicked"))

    window.show()
    sys.exit(app.exec_())

这篇关于读取 UI 文件并连接按钮、文本等元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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