我是 Python 中 Qt 的初学者。
我使用 Qt Designer 创建简单的东西。
我需要的 - 在用户点击按钮后,应用将文本从编辑复制到标签。
我有来自 Qt Designer 的文件 example.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>308</width>
<height>143</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>10</x>
<y>20</y>
<width>121</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>Enter name</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>100</x>
<y>20</y>
<width>113</width>
<height>27</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>80</x>
<y>60</y>
<width>85</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string>Display</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>10</x>
<y>90</y>
<width>261</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
如何在我的 Python 代码中使用它?
我修改了一些教程中的代码并且它有效:
import sys
from PyQt4 import QtCore, QtGui, uic
form_class = uic.loadUiType("example.ui")[0]
class MyWindowClass(QtGui.QMainWindow, form_class):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.pushButton_clicked)
def pushButton_clicked(self):
input = self.lineEdit.text()
self.label_2.setText(input)
app = QtGui.QApplication(sys.argv)
myWindow = MyWindowClass(None)
myWindow.show()
app.exec_()
但是代码完成不起作用!所以它对我来说无法使用:-(
我正在使用 JetBrains Pycharm。
在 IDE 中使用工作代码竞争在 python 代码中使用 Qt 设计器输出的正确方法是什么?
最佳答案
不是一个完整的答案,但肯定值得一提:代码完成不适用于动态对象。你当然仍然可以使用
self.pushButton.clicked.connect(self.abc)
代替
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.abc)
但是 self.pushButton.clicked.*
不会有任何代码完成
(这也回答了问题 https://stackoverflow.com/a/28270242/4537483 )
关于python - Pycharm:Python Qt代码代码补全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28269883/