有没有办法更改lcdNumber?

时间:2019-08-17 17:02:41

标签: python python-3.x qt-designer pyside2

我正在创建一个GUI,我需要2x LCDNumber,它们分别是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面板上更改其编号。但事实并非如此。

1 个答案:

答案 0 :(得分:1)

在使用QUiLoader时,使用对象名将对象映射为窗口属性,

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