python - 我在哪里输入我的代码(Pyside2)?
问题描述
您好,我在 qt 设计器中制作了一个 ui 文件,并将其转换为 py 文件,但我想知道在哪里可以输入我的按钮功能在这个程序中
主文件
import sys
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import QFile
if __name__ == "__main__":
app = QApplication(sys.argv)
file = QFile("mainwindow.ui")
file.open(QFile.ReadOnly)
loader = QUiLoader()
window = loader.load(file)
window.show()
sys.exit(app.exec_())
*.ui
<?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>800</width>
<height>599</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>40</x>
<y>10</y>
<width>231</width>
<height>61</height>
</rect>
</property>
<property name="font">
<font>
<family>Arial Black</family>
<pointsize>18</pointsize>
</font>
</property>
<property name="text">
<string>Press this button</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>60</x>
<y>80</y>
<width>181</width>
<height>91</height>
</rect>
</property>
<property name="font">
<font>
<family>Arial Black</family>
<pointsize>16</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Press</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
那么你能否指出我可以从哪里开始编写按钮等功能,
提前致谢
解决方案
您必须做的是将按钮的单击信号连接到某个函数,但为此您必须知道函数的名称,为此我们转到 Object Inspector:
在那里我们看到按钮的名称是pushButton
。所以使用显示代码的main.py如下:
import sys
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import QFile
def foo():
print("clicked")
if __name__ == "__main__":
app = QApplication(sys.argv)
file = QFile("mainwindow.ui")
if not file.open(QFile.ReadOnly):
sys.exit(-1)
loader = QUiLoader()
window = loader.load(file)
window.pushButton.clicked.connect(foo)
window.show()
sys.exit(app.exec_())
推荐阅读
- javascript - Javascript DOM 制作表格
- javascript - 如何使用数组方法对数组中对象的属性求和
- c - C中的Minishell“分段错误,核心转储”错误
- python - Tkinter 默认字体很小
- python - 无法使用 os python 获取环境变量
- python - 制作二十一点游戏:如何为 AJQK 赋值
- swift - SwiftUI - 基于设备的自定义 TabBar 高度
- python - 'Pip' 未被识别为内部或外部命令 windows 10 CMD
- docker - 连接时出错:系统找不到指定的文件..,必须提升 docker 客户端才能连接
- azure - Microsoft Azure - 我使用 Azure AD 门户添加的自定义域在哪里?