Search code examples
pythonpython-3.xqt-designerpyside2

Is there a way to change the lcdNumber?


I am creating a GUI and I need 2x LCDNumbers which are either 0,8,12. I created the .ui file with QTDesigner 5.9.8 and loaded it into python3 with pyside2.

I need to change the LCDNumbers (QLCDNumber) but don`t know how. Was trying it several ways but didnt work.

Tryed several methods with PySide2.QtWidget.QLCDNumber(),QObject,display()

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

def main():
    app = QApplication(sys.argv)
    ui_file = QFile("mainwindow.ui")
    ui_file.open(QFile.ReadWrite)
    loader = QUiLoader()
    window = loader.load(ui_file)
    ui_file.close()
    window.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

the mainwindow.ui file

<?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>653</width>
    <height>477</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>50</x>
      <y>20</y>
      <width>211</width>
      <height>121</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>30</pointsize>
     </font>
    </property>
    <property name="text">
     <string>Coater 1</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>380</x>
      <y>20</y>
      <width>211</width>
      <height>121</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>30</pointsize>
     </font>
    </property>
    <property name="text">
     <string>Coater 2</string>
    </property>
   </widget>
   <widget class="QLCDNumber" name="lcdNumber_1">
    <property name="enabled">
     <bool>true</bool>
    </property>
    <property name="geometry">
     <rect>
      <x>70</x>
      <y>200</y>
      <width>151</width>
      <height>101</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>8</pointsize>
     </font>
    </property>
    <property name="contextMenuPolicy">
     <enum>Qt::NoContextMenu</enum>
    </property>
    <property name="inputMethodHints">
     <set>Qt::ImhNone</set>
    </property>
    <property name="value" stdset="0">
     <double>8.000000000000000</double>
    </property>
    <property name="intValue" stdset="0">
     <number>8</number>
    </property>
   </widget>
   <widget class="QLCDNumber" name="lcdNumber_2">
    <property name="geometry">
     <rect>
      <x>400</x>
      <y>200</y>
      <width>151</width>
      <height>101</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>8</pointsize>
     </font>
    </property>
    <property name="value" stdset="0">
     <double>12.000000000000000</double>
    </property>
   </widget>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

I expected the output to change its number at the LCD-Panel. But it doesn't.


Solution

  • When you use QUiLoader objects are mapped as window attributes using objectnames, in your case:

    • <widget class="QLCDNumber" name="lcdNumber_1">

    • <widget class="QLCDNumber" name="lcdNumber_2">

    So you must use "lcdNumber_1" and "lcdNumber_2" to access the QLCDNumber:

    import os
    import sys
    
    from PySide2.QtCore import QFile
    from PySide2.QtWidgets import QApplication
    from PySide2.QtUiTools import QUiLoader
    
    
    def main():
        app = QApplication(sys.argv)
        current_dir = os.path.dirname(os.path.realpath(__file__))
        ui_file = QFile(os.path.join(current_dir, "mainwindow.ui"))
        if ui_file.open(QFile.ReadOnly):
            loader = QUiLoader()
            window = loader.load(ui_file)
            ui_file.close()
            window.show()
            window.lcdNumber_1.display(100)
            window.lcdNumber_2.display(200)
            sys.exit(app.exec_())
    
    
    if __name__ == "__main__":
        main()