问题
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.
I see the difference between PyQt5 and PySide2:
https://www.learnpyqt.com/blog/pyqt5-vs-pyside2/
but am unclear on how a button would hook up in using PySide2.
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_()
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>
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_()
回答1:
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_())
来源:https://stackoverflow.com/questions/61882731/reading-ui-file-and-connecting-elements-such-as-buttons-text-etc