Search code examples
pythonpython-3.xpyside2

Where Do I input my code(Pyside2)?


Hello there I have made a ui file in qt designer that I converted into a py file but I was wondering where I can input my functions for buttons in this program

main.py

import sys
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import QFile

if __name__ == "__main__":
    app = QApplication(sys.argv)

file = QFile("mainwindow.ui")
file.open(QFile.ReadOnly)

loader = QUiLoader()
window = loader.load(file)
window.show()

sys.exit(app.exec_())

*.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>800</width>
    <height>599</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>40</x>
      <y>10</y>
      <width>231</width>
      <height>61</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>Arial Black</family>
      <pointsize>18</pointsize>
     </font>
    </property>
    <property name="text">
     <string>Press this button</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>80</y>
      <width>181</width>
      <height>91</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>Arial Black</family>
      <pointsize>16</pointsize>
      <weight>75</weight>
      <bold>true</bold>
     </font>
    </property>
    <property name="text">
     <string>Press</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

So can you please indicate where I can start writing functions for buttons and so on,

Thanks in Advance


Solution

  • What you must do is connect the clicked signal of the button to some function, but for that you must know the name of the function, for that we go to Object Inspector:

    enter image description here

    There we see that the name of the button is pushButton. So using the main.py that shows the code is as follows:

    import sys
    from PySide2.QtUiTools import QUiLoader
    from PySide2.QtWidgets import QApplication
    from PySide2.QtCore import QFile
    
    def foo():
        print("clicked")
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
    
        file = QFile("mainwindow.ui")
        if not file.open(QFile.ReadOnly):
            sys.exit(-1)
    
        loader = QUiLoader()
        window = loader.load(file)
        window.pushButton.clicked.connect(foo)
        window.show()
    
        sys.exit(app.exec_())