动态添加时如何设置对象的名称?
How to set the name of an object when adding dynamically?
我正在创建一个卡路里计算器,我需要动态添加食物。我正在动态添加小部件。例如,我添加一个标签。但是在 setObjectName 之后,我无法通过该名称访问。例如,我将标签的名称设置为“名称”。我试图用下图 name.text () 按名称获取标签的文本,但代码崩溃了。我做错了什么?
代码如下:
import sys # interaction with Python
from PyQt5.QtWidgets import * # for classic application based on widgets
from PyQt5 import uic # to read ui file
from PyQt5 import QtWidgets # to create gui
class MyWin(QtWidgets.QMainWindow): # create class witch inherit QMainWindow
def __init__(self): # constructor
QtWidgets.QMainWindow.__init__(self) # constructor of parent class
uic.loadUi("gui.ui", self) # load ui
self.add_product.clicked.connect(self.add)
self.remove_product.clicked.connect(self.remove)
def add(self):
h1 = QHBoxLayout()
self.label = QLabel()
self.label.setObjectName("name") # set name
self.label.text = "L"
h1.addWidget(self.label)
h1.addWidget(QLabel('Weight'))
h2 = QHBoxLayout()
h2.addWidget(QLineEdit())
h2.addWidget(QLineEdit())
i = self.verticalLayout_2.count()
self.verticalLayout_2.insertLayout(i - 2, h1)
self.verticalLayout_2.insertLayout(i - 1, h2)
print(self.name.text()) # ERROR
def remove(self):
i = self.verticalLayout_2.count()
if i > 3:
QWidget().setLayout(self.verticalLayout_2.takeAt(i - 3))
QWidget().setLayout(self.verticalLayout_2.takeAt(i - 4))
if __name__ == '__main__': # for check non import module
app = QApplication(sys.argv) # create app
mw = MyWin() # create object of MyWin class
mw.show() # to show gui
sys.exit(app.exec_()) # execute app
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>458</width>
<height>234</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="breakfest">
<property name="title">
<string>Breakfest</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_product">
<property name="text">
<string>Product</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_weight">
<property name="text">
<string>Weight</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="lineEdit_product"/>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_weight"/>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QPushButton" name="add_product">
<property name="text">
<string>Add product</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="remove_product">
<property name="text">
<string>Remove product</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
您不能简单地使用 objectName 访问对象。您需要使用 findChild
方法。
选项 1:
self.widget_i_want = self.findChild(QLabel, "name")
但是正如@ekhumoro 所说,最好使用列表或字典来存储所有添加的标签。
选项 2:
class MyWin(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
uic.loadUi("gui.ui", self) # load ui
self.counter = 1 #this is going to be used to set unique names
self.add_product.clicked.connect(self.add)
self.remove_product.clicked.connect(self.remove)
self.dynamically_added_widgets = list()
def add(self):
global counter
self.counter += 1
h1 = QHBoxLayout()
self.label = QLabel()
self.label.setObjectName(f"name{self.counter}") # set a new, unique name
self.label.text = "L"
h1.addWidget(self.label)
h1.addWidget(QLabel('Weight'))
h2 = QHBoxLayout()
h2.addWidget(QLineEdit())
h2.addWidget(QLineEdit())
i = self.verticalLayout_2.count()
self.verticalLayout_2.insertLayout(i - 2, h1)
self.verticalLayout_2.insertLayout(i - 1, h2)
self.dynamically_added_widgets.append(self.label) # add the new label to list
print(self.dynamically_added_widgets)
print(self.dynamically_added_widgets[-1].objectName()) # print the last added label's objectName
def remove(self):
i = self.verticalLayout_2.count()
if i > 3:
QWidget().setLayout(self.verticalLayout_2.takeAt(i - 3))
QWidget().setLayout(self.verticalLayout_2.takeAt(i - 4))
我正在创建一个卡路里计算器,我需要动态添加食物。我正在动态添加小部件。例如,我添加一个标签。但是在 setObjectName 之后,我无法通过该名称访问。例如,我将标签的名称设置为“名称”。我试图用下图 name.text () 按名称获取标签的文本,但代码崩溃了。我做错了什么?
代码如下:
import sys # interaction with Python
from PyQt5.QtWidgets import * # for classic application based on widgets
from PyQt5 import uic # to read ui file
from PyQt5 import QtWidgets # to create gui
class MyWin(QtWidgets.QMainWindow): # create class witch inherit QMainWindow
def __init__(self): # constructor
QtWidgets.QMainWindow.__init__(self) # constructor of parent class
uic.loadUi("gui.ui", self) # load ui
self.add_product.clicked.connect(self.add)
self.remove_product.clicked.connect(self.remove)
def add(self):
h1 = QHBoxLayout()
self.label = QLabel()
self.label.setObjectName("name") # set name
self.label.text = "L"
h1.addWidget(self.label)
h1.addWidget(QLabel('Weight'))
h2 = QHBoxLayout()
h2.addWidget(QLineEdit())
h2.addWidget(QLineEdit())
i = self.verticalLayout_2.count()
self.verticalLayout_2.insertLayout(i - 2, h1)
self.verticalLayout_2.insertLayout(i - 1, h2)
print(self.name.text()) # ERROR
def remove(self):
i = self.verticalLayout_2.count()
if i > 3:
QWidget().setLayout(self.verticalLayout_2.takeAt(i - 3))
QWidget().setLayout(self.verticalLayout_2.takeAt(i - 4))
if __name__ == '__main__': # for check non import module
app = QApplication(sys.argv) # create app
mw = MyWin() # create object of MyWin class
mw.show() # to show gui
sys.exit(app.exec_()) # execute app
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>458</width>
<height>234</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="breakfest">
<property name="title">
<string>Breakfest</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_product">
<property name="text">
<string>Product</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_weight">
<property name="text">
<string>Weight</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="lineEdit_product"/>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_weight"/>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QPushButton" name="add_product">
<property name="text">
<string>Add product</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="remove_product">
<property name="text">
<string>Remove product</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
您不能简单地使用 objectName 访问对象。您需要使用 findChild
方法。
选项 1:
self.widget_i_want = self.findChild(QLabel, "name")
但是正如@ekhumoro 所说,最好使用列表或字典来存储所有添加的标签。
选项 2:
class MyWin(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
uic.loadUi("gui.ui", self) # load ui
self.counter = 1 #this is going to be used to set unique names
self.add_product.clicked.connect(self.add)
self.remove_product.clicked.connect(self.remove)
self.dynamically_added_widgets = list()
def add(self):
global counter
self.counter += 1
h1 = QHBoxLayout()
self.label = QLabel()
self.label.setObjectName(f"name{self.counter}") # set a new, unique name
self.label.text = "L"
h1.addWidget(self.label)
h1.addWidget(QLabel('Weight'))
h2 = QHBoxLayout()
h2.addWidget(QLineEdit())
h2.addWidget(QLineEdit())
i = self.verticalLayout_2.count()
self.verticalLayout_2.insertLayout(i - 2, h1)
self.verticalLayout_2.insertLayout(i - 1, h2)
self.dynamically_added_widgets.append(self.label) # add the new label to list
print(self.dynamically_added_widgets)
print(self.dynamically_added_widgets[-1].objectName()) # print the last added label's objectName
def remove(self):
i = self.verticalLayout_2.count()
if i > 3:
QWidget().setLayout(self.verticalLayout_2.takeAt(i - 3))
QWidget().setLayout(self.verticalLayout_2.takeAt(i - 4))