【问题标题】:access element from .ui从 .ui 访问元素
【发布时间】: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


【解决方案1】:

您混淆了以下概念:

  • 您正在创建 UI 类,您可以在其中创建连接但从不使用它。你认为某事是如何运作的?

  • 您不需要使用 findChild(),因为如果您使用 loadUi 或 loadUiType,它将使用 objectName 映射对象。

综合以上情况,解决方案如下:

loadUi():

from PyQt5 import uic, QtWidgets


class Ui(QtWidgets.QDialog):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi("dialog.ui", self)
        self.qpbSearch.clicked.connect(self.printButtonPressed)

    def printButtonPressed(self):
        self.qlFreeText.setText("hello")

        alert = QtWidgets.QMessageBox()
        alert.setText("You clicked the button!")
        alert.exec_()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Ui()
    w.show()  # show window
    sys.exit(app.exec_())

loadUiType():

from PyQt5 import uic, QtWidgets

Form, _ = uic.loadUiType("dialog.ui")


class Ui(QtWidgets.QDialog, Form):
    def __init__(self):
        super(Ui, self).__init__()
        self.setupUi(self)
        self.qpbSearch.clicked.connect(self.printButtonPressed)

    def printButtonPressed(self):
        self.qlFreeText.setText("hello")

        alert = QtWidgets.QMessageBox()
        alert.setText("You clicked the button!")
        alert.exec_()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Ui()
    w.show()  # show window
    sys.exit(app.exec_())

我建议你查看Using Qt Designer

【讨论】:

  • 我强烈反对使用 Qt 设计器检查它为代码生成的东西是可怕的——从一些高质量的在线免费教程中学习如何创建一个基本窗口会更好。那里
  • @DennisJensen 这就是你想要的,但这不是 OP 要求的。我想要很多东西,但不幸的是,并非一切皆有可能。
  • @DennisJensen 我不是导师,我不假装是,所以也不是故意的,这可能是结果,但不是目标。在这么小的空间里,我会说不可能尝试成为一名导师是很困难的。一个更接近你想要的地方可能是codereview.stackexchange.com
  • 不,@eyllanesc 他们没有问这个问题——他们只是说他们正在使用 Qt Designer,他们可能正在使用它,因为他们不知道更好,他们不应该使用它——但是您完全了解它的众多问题,但您通过将他们指向那个方向来伤害个人??
  • @DennisJensen 你是什么意思?我没遇到过问题,可能你的意思是没看工具说明书造成的问题,不要怪工具用的人不知道怎么用。我不知道?好吧,我知之甚少,让我走上邪恶的道路吧。
【解决方案2】:
from PyQt5 import QtWidgets, uic
import sys


def clicked_me():
    print("You Clicked Me! :-)")


app = QtWidgets.QApplication([])
win = uic.loadUi("main_window.ui")

win.btn_start_capture.clicked.connect(clicked_me)
# My Button's Name is btn_start_capture

win.show()
sys.exit(app.exec())

【讨论】:

    【解决方案3】:

    在我的文件中,我就是这样做的。

    加载用户界面

    self.ui = uic.loadUi('pyqt-gui-form.ui', self)
    

    对象的名称是save_credentials_btn

    self.ui.save_credentials_btn.clicked.connect(self.save_user)
    

    整个文件

    from PyQt6.QtWidgets import QApplication, QWidget
    import sys # This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available.
    from PyQt6 import uic
    
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
            # LOAD UI FILE
            self.ui = uic.loadUi('pyqt-gui-form.ui', self)
            self.ui.save_credentials_btn.clicked.connect(self.save_user)
    
        def save_user(self):
            print("Save User")
    
    app = QApplication([])
    window = Window()
    window.show()
    sys.exit(app.exec())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      • 2021-11-08
      相关资源
      最近更新 更多