首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有办法改变lcdNumber吗?

有办法改变lcdNumber吗?
EN

Stack Overflow用户
提问于 2019-08-17 17:02:41
回答 1查看 291关注 0票数 0

我正在创建一个GUI,我需要2x LCDNumbers,它要么是0,8,12。我用QTDesigner 5.9.8创建了.ui文件,然后用pyside2将它加载到python3中。

我需要改变LCDNumbers (QLCDNumber),但不知道如何改变。尝试了几种方法,但都没有用。

使用PySide2.QtWidget.QLCDNumber()、QObject、display()尝试了几种方法

代码语言:javascript
复制
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文件

代码语言:javascript
复制
<?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面板上改变其编号.但事实并非如此。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-17 21:34:00

当您使用QUiLoader对象时,使用对象名将对象映射为窗口属性,在您的示例中:

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

因此,您必须使用"lcdNumber_1“和"lcdNumber_2”来访问QLCDNumber:

代码语言:javascript
复制
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()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57538309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档