access element from .ui

扶醉桌前 提交于 2021-02-10 20:25:52

问题


I have problem to access my button and label from my dialog.ui. I am using Python 3.x and 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>

When I click on the button, nothing happens. What I would like to try is, when I click on the button, than it changes the label text. But currently I can even not use the click on the button.


回答1:


You are confusing the following concepts:

  • You are creating the UI class where you create the connection but never use it. How do you think something works?

  • You don't need to use findChild() since if you use loadUi or loadUiType it will map the objects using the objectName.

Considering the above, the solutions are as follows:

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_())

I recommend you check Using Qt Designer.




回答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())


来源:https://stackoverflow.com/questions/57461720/access-element-from-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!