有没有办法改变 lcdNumber?

Is there a way to change the lcdNumber?

我正在创建一个 GUI,我需要 2x LCDNumbers,它们是 0、8、12。 我使用 QTDesigner 5.9.8 创建了 .ui 文件,并使用 pyside2.

将其加载到 python3

我需要更改 LCDNumbers (QLCDNumber) 但不知道如何更改。尝试了几种方法但没有奏效。

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

mainwindow.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>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>

我预计输出会在 LCD 面板上更改其编号。但事实并非如此。

当您使用 QUiLoader 时,对象被映射为使用对象名称的 window 属性,在您的情况下:

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

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

所以你必须使用"lcdNumber_1"和"lcdNumber_2"来访问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()