【发布时间】:2019-08-12 13:00:53
【问题描述】:
我无法从我的 dialog.ui 访问我的按钮和标签。我正在使用 Python 3.x 和 QT Designer 5.x。
from PyQt5 import uic, QtWidgets
from PyQt5.QtWidgets import QApplication
Form, Window = uic.loadUiType("dialog.ui") #load ui (GUI) file
app = QApplication([]) #create a QApplication
window = Window()
form = Form()
form.setupUi(window)
def on_click():
# self.qlFreeText.text("hello")
alert = QMessageBox()
alert.setText("You clicked the button!")
alert.exec_()
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi('basic.ui',self)
# self.ButtonSearch = self.findChild(QtWidgets.QPushButton, 'qpbSearch')
self.ButtonSearch = self.findChild(QtWidgets.QObject, 'qpbSearch')
self.ButtonSearch.button.clicked.connect(self.printButtonPressed)
self.qlFreeText = self.findChild(QWidgets.QLabel, 'qlFreeText')
# self.show()
def printButtonPressed(self):
on_click()
window.show() #show window
app.exec_() #run application until user closes it
dialog.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QDateEdit" name="dateStart">
<property name="geometry">
<rect>
<x>50</x>
<y>50</y>
<width>110</width>
<height>22</height>
</rect>
</property>
<property name="displayFormat">
<string>yyyy-MM-dd</string>
</property>
</widget>
<widget class="QDateEdit" name="dateEnd">
<property name="geometry">
<rect>
<x>220</x>
<y>50</y>
<width>110</width>
<height>22</height>
</rect>
</property>
<property name="displayFormat">
<string>yyyy-MM-dd</string>
</property>
</widget>
<widget class="QLabel" name="qlFreeText">
<property name="geometry">
<rect>
<x>120</x>
<y>140</y>
<width>55</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QPushButton" name="qpbSearch">
<property name="geometry">
<rect>
<x>190</x>
<y>220</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
当我点击按钮时,什么也没有发生。我想尝试的是,当我单击按钮时,它会更改标签文本。但目前我什至不能使用点击按钮。
【问题讨论】:
-
分享 basic.ui 文件。 basic.ui 还是 dialog.ui?
-
你在哪里使用
Ui类? -
@eyllanesc,非常感谢您的帮助。我明白了,我误解了python和pyqt之间的联系。上面我添加了新代码,现在可以使用了。为了更好地理解事物,我仍在玩一点“类”。也感谢链接“使用 Qt 设计器”。这很有帮助。新代码肯定不完美!!还在学习中...
-
我仍在尝试不使用“全局”,但还没有找到任何其他解决方案...
标签: python python-3.x pyqt pyqt5 qt-designer