Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用PySide2更改Qt.ui文件中的组合框值_Python_Qt Designer_Pyside2 - Fatal编程技术网

Python 使用PySide2更改Qt.ui文件中的组合框值

Python 使用PySide2更改Qt.ui文件中的组合框值,python,qt-designer,pyside2,Python,Qt Designer,Pyside2,我正在使用PySide2加载一个Qt Designer.ui文件。我有一个名为categoryBox的组合框,它是MainWindow.centralwidget的子项 我希望使用Python将其内容替换为列表,而不更改.ui文件本身。但是,我不知道如何找到组合框,这在替换时很重要 简而言之,我有一个组合框。我不知道在哪里能找到它,但我知道它在哪里 .ui文件中的相关XML数据: <?xml version="1.0" encoding="UTF-8"

我正在使用PySide2加载一个Qt Designer.ui文件。我有一个名为categoryBox的组合框,它是MainWindow.centralwidget的子项

我希望使用Python将其内容替换为列表,而不更改.ui文件本身。但是,我不知道如何找到组合框,这在替换时很重要

简而言之,我有一个组合框。我不知道在哪里能找到它,但我知道它在哪里

.ui文件中的相关XML数据:

<?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>799</width>
    <height>513</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>BlueCalculator</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="titleLabel">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>10</y>
      <width>231</width>
      <height>51</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>26</pointsize>
     </font>
    </property>
    <property name="text">
     <string>BlueCalculator</string>
    </property>
   </widget>
   <widget class="QComboBox" name="categoryBox">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>80</y>
      <width>221</width>
      <height>21</height>
     </rect>
    </property>
   </widget>
import sys, os
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import QFile, QIODevice
import os
rootdir = '.\\functions'


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

ui_file_name = "main.ui"
ui_file = QFile(ui_file_name)
if not ui_file.open(QIODevice.ReadOnly):
    print("Cannot open {}: {}".format(ui_file_name, ui_file.errorString()))
    sys.exit(-1)
loader = QUiLoader()
window = loader.load(ui_file)
ui_file.close()
if not window:
    print(loader.errorString())
    sys.exit(-1)
window.show()

sys.exit(app.exec_())

使用QUiLoader时,所有对象都将使用objectName指定为窗口的属性,在您的情况下,您应该使用:

window.categoryBox.addItems(["Foo", "Bar"])