1

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Alain Staub
  • 13
  • 1
  • 4
  • share the basic.ui file. basic.ui or dialog.ui? – eyllanesc Aug 12 '19 at 13:02
  • 1
    where do you use the `Ui` class? – eyllanesc Aug 12 '19 at 14:38
  • @eyllanesc, thank you very much for your help. I see, I misunderstood the connection between python and pyqt. Above I added the new code, which works now. I am still playing a little bit with 'class' to understand the things better. Thanks as well for the link "Using Qt Designer". It was helpful. The new code is for sure not perfect!! Still learning... – Alain Staub Aug 16 '19 at 05:21
  • And I am still trying to not use 'global' but did not find any other solution yet... – Alain Staub Aug 16 '19 at 05:26
  • read [Why are global variables evil?](https://stackoverflow.com/questions/19158339) – eyllanesc Aug 16 '19 at 05:29

3 Answers3

3

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I would strongly disagree with checking out using the Qt Designer what it produces for code is horrible -- you would be better served just learning how to create a basic window from some of the quality online free tutorials out there – Dennis Jensen Aug 12 '19 at 16:58
  • @DennisJensen That's what you want, but it's not what the OP asks. I would like many things but unfortunately not everything is possible. – eyllanesc Aug 12 '19 at 17:00
  • @DennisJensen I am not a tutor and I do not pretend to be, and SO is not intended to be, it may be a consequence but it is not the objective. In such a small space it is difficult, I would say impossible, to try to be a tutor. A place closer to what you want could be https://codereview.stackexchange.com/ – eyllanesc Aug 12 '19 at 17:06
  • No @eyllanesc they did not ask that -- they just stated they were using Qt Designer and they are probably using it because they do not know any better and that they ought not to use it -- but you are perfectly aware of its numerous issues and yet you do the individual a disservice by pointing them in that direction?? – Dennis Jensen Aug 12 '19 at 17:08
  • @DennisJensen What problems do you mean? I have not had problems, maybe you mean the problems that are caused by not reading the tool manual, do not blame the tool if he who uses it does not know how to use it. I do not know? Well, I know little, let me go to the path of evil. – eyllanesc Aug 12 '19 at 17:11
  • Note please do not get me wrong -- I actually have a lot of respect for you as a programmer -- and it is because of that I do more than just ignore you -- as I see the potential for you making an even greater positive impact on all the lives you touch via this medium – Dennis Jensen Aug 12 '19 at 17:11
  • @DennisJensen I am pragmatic: If something helps me I use it, if it doesn't help me then no. – eyllanesc Aug 12 '19 at 17:14
  • I have a problem with bad code (as all programmers do if they have to work with it) -- the Designer produces bad code so yes by extension I have issues with it due to that -- and YES there ARE bad practices when it comes to coding its why things like memory leaks occur and numerous other bugs are introduced – Dennis Jensen Aug 12 '19 at 17:15
  • @DennisJensen My experience tells me that the tools are neither good nor bad, the benefit of them depends on how they use them. – eyllanesc Aug 12 '19 at 17:15
  • @DennisJensen mmmm, bad code? Memory leaks ?, I find it very strange what you point out. I think we are not going to reach anything in particular, we are already discussing the same. I will do what is right for me, I advise you to do the same, and I will not change it. goodbye. – eyllanesc Aug 12 '19 at 17:18
  • Okay let see tools are neither good nor bad the benefit of them depends on how they are used -- interesting stance -- you will eventually understand that all tools are not made equally and that there are truly bad tools that exit and further that these bad tools get used by folks that do not know any better. It is not an uncommon occurrence. It takes those in the know to help those not in the know so that they do not use these bad tools -- then either someone fixes the tool and makes it better or the tool fades into obscurity either is better than promoting a bad tool which the Designer is. – Dennis Jensen Aug 12 '19 at 17:24
  • @DennisJensen We get to the same point: For you, Qt Designer is bad and for me it works correctly, so I don't share your point of view so I will not listen to you. This discussion tends to be unproductive since we have very different views. – eyllanesc Aug 12 '19 at 17:26
  • Yes that sadly is true and while you can get a bad tool to work it does not by any means make it a good tool -- it is still a bad tool that ought not to be used by anyone -- but you misunderstand my purpose -- I am not trying to convince you to change as that is most like a effort in futility -- what I am trying to do is help the OP by letting them know that using the Designer is not what they should be doing and that if they continue to use it they can expect to have more issues down the road because of it and if this helps even one person then it was worth my time. – Dennis Jensen Aug 12 '19 at 17:34
  • @DennisJensen Then do that in your answer or in the comments section of the OP. – eyllanesc Aug 12 '19 at 17:36
  • @DennisJensen I think you have taken my comment badly, I want to say is that if you want to point something to the OP, it is to say it directly to him and not to me, for this you can use the comments section below the OP's question or your answer. I respect everyone regardless of their contributions or have a different point of view than mine. – eyllanesc Aug 12 '19 at 17:40
1
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())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
0

In my file, This is how I did it.

Loading User Interface

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

The name of object is save_credentials_btn

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

Whole file

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())
MD SHAYON
  • 7,001
  • 45
  • 38