英文:
Get value of input fields using PySide2
问题
I'll provide the translated code parts for you:
我正试图使用PySide2与对话框按钮。
对话框窗口上的每列都有一个复选框、一个按钮和一个标签。
单击按钮“设置文本”会打开另一个对话框以输入文本。我想要获取此对话框的输入值(只需输入此值),并更新单击按钮旁边的标签。在这种情况下,将第一个标签的值更改为“设置值”为“只需输入此”。因为我点击了第一个“设置文本”按钮。
最后,通过单击“应用”按钮,我想获取复选框的值以及标签(位于每行末尾的“设置值”标签)。
以下是对话框和代码:
来自PySide2.QtWidgets的导入
(QApplication,QPushButton,
QWidget,QLabel,QVBoxLayout,QHBoxLayout,QLineEdit,QInputDialog,QCheckBox)
import sys
来自PySide2的导入QtWidgets
class AnotherWindow(QWidget):
""""
这个“窗口”是一个QWidget。如果没有父级,它
将显示为自由浮动窗口,正如我们所希望的那样。
"""
def __init__(self):
super().__init__()
def init_input_dialog(self):
text, ok = QInputDialog.getText(self, '文本输入对话框', '输入内容:')
if ok:
return str(text)
class Window(QWidget):
def __init__(self):
super().__init__()
v = QVBoxLayout()
objects = ['标签1', '标签2']
for obj in objects:
h = QHBoxLayout()
check_box = QCheckBox()
labels = QLabel(obj)
button = QPushButton("设置文本")
button.pressed.connect(
lambda val=obj: self.show_new_window(val)
)
label_value = QLabel("设置值", objectName=obj)
h.addWidget(check_box)
h.addWidget(labels)
h.addWidget(button)
h.addWidget(label_value)
# 将当前对象的条目添加到主布局
v.addLayout(h)
# 添加按钮:确定和取消
btn_ok = QPushButton("应用")
btn_ok.clicked.connect(self.get_dialog_box_values)
btn_cancel = QPushButton("取消")
btn_cancel.clicked.connect(self.close_dialog_box())
h = QHBoxLayout()
h.addWidget(btn_ok)
h.addWidget(btn_cancel)
v.addLayout(h)
# 设置布局
self.setLayout(v)
def show_new_window(self, n):
win = AnotherWindow()
val = win.init_input_dialog()
print(val)
# 用此值替换相应的标签
# label_value.setText(str(val))
def get_dialog_box_values(self):
# 获取复选框的状态和标签值
check_box_values = []
label_values = []
print(check_box_values)
print(label_values)
def close_dialog_box(self):
# 关闭对话框
pass
app = QApplication(sys.argv)
w = Window()
w.show()
app.exec_()
Please note that I've translated the comments in the code as well. Let me know if you need further assistance or have any questions.
英文:
I am trying to use PySide2 with the dialog buttons.
Each column on the dialogWindow has a chexbox, a button and a label.
Clicking on the button Set Text
opens another dialog box to enter a text. I would like to get the input value of this dialog box (just enter this
), and update the label next to the clicking button. In this case, changing the first label value Set value
to just enter this
, as I clicked on the first Set text
button.
Finally, by clicking on the "Apply" button, I would like to get the values of the check boxes, and the labels (those at the end of each row with Set value
).
Below are the dialog boxes and the code
from PySide2.QtWidgets import (
QApplication, QPushButton,
QWidget, QLabel, QVBoxLayout, QHBoxLayout, QLineEdit, QInputDialog, QCheckBox
)
import sys
from PySide2 import QtWidgets
class AnotherWindow(QWidget):
"""
This "window" is a QWidget. If it has no parent, it
will appear as a free-floating window as we want.
"""
def __init__(self):
super().__init__()
def init_input_dialog(self):
text, ok = QInputDialog.getText(self, 'Text Input Dialog', 'Enter something:')
if ok:
return str(text)
class Window(QWidget):
def __init__(self):
super().__init__()
v = QVBoxLayout()
objets = ['label1', 'label2']
for a in objets:
h = QHBoxLayout()
check_box = QCheckBox()
labels = QLabel( a )
button = QPushButton("Set text")
button.pressed.connect(
lambda val=a: self.show_new_window(val)
)
label_value = QLabel( "Set value", objectName=a )
h.addWidget(check_box)
h.addWidget(labels)
h.addWidget(button)
h.addWidget(label_value)
# Add the current objet entry to the main layout
v.addLayout(h)
# Add buttons: Ok and Cancel
btn_ok = QPushButton("Apply")
btn_ok.clicked.connect(self.get_dialog_box_values)
btn_cancel = QPushButton("Cancel")
btn_cancel.clicked.connect(self.close_dialog_box())
h = QHBoxLayout()
h.addWidget(btn_ok)
h.addWidget(btn_cancel)
v.addLayout(h)
# Set the layout
self.setLayout(v)
def show_new_window(self, n):
win = AnotherWindow()
val = win.init_input_dialog()
print(val)
# Replace the corresponding label with this value
# label_value.setText(str(val))
def get_dialog_box_values(self):
# Get state of checkboxes, and label values
check_box_values = []
label_values = []
print(check_box_values)
print(label_values)
def close_dialog_box(self):
# Close the dialog box
pass
app = QApplication(sys.argv)
w = Window()
w.show()
app.exec_()
I am also open to any suggestions on how to achieve this in a better way.
e.g. if there is a better way to add the Apply
and Cancel
buttons to the main dialog box.
答案1
得分: 1
你需要引用所有创建并放置到布局中的小部件。在您的示例和描述中,至少需要能够与每一行末尾的标签以及复选框标签进行交互,因此这些小部件是您应该保留引用的小部件。您可以通过将它们作为窗口类的属性来实现这一点,通过给它们赋予self
赋值。这是面向对象编程世界中的一个重要概念之一。
一旦它们被分配为窗口类的属性,您就可以在该类的任何实例方法中使用相同的self
对象引用它们。
例如:
...
...
class Window(QWidget):
def __init__(self):
super().__init__()
vlayout = QVBoxLayout()
hlayout1 = QHBoxLayout()
hlayout2 = QHBoxLayout()
self.check_box1 = QCheckBox("label1")
self.check_box2 = QCheckBox("label2")
button1 = QPushButton("Set text")
button2 = QPushButton("Set text")
self.label_value1 = QLabel("Set value")
self.label_value2 = QLabel("Set value")
button1.pressed.connect(lambda: self.show_new_window(self.label_value1))
button2.pressed.connect(lambda: self.show_new_window(self.label_value2))
hlayout1.addWidget(self.check_box1)
hlayout2.addWidget(self.check_box2)
hlayout1.addWidget(button1)
hlayout2.addWidget(button2)
hlayout1.addWidget(self.label_value1)
hlayout2.addWidget(self.label_value2)
# 将当前对象添加到主布局
vlayout.addLayout(hlayout1)
vlayout.addLayout(hlayout2)
# 添加按钮:应用和取消
btn_ok = QPushButton("Apply")
btn_ok.clicked.connect(self.get_dialog_box_values)
btn_cancel = QPushButton("Cancel")
btn_cancel.clicked.connect(self.close_dialog_box)
hlayout3 = QHBoxLayout()
hlayout3.addWidget(btn_ok)
hlayout3.addWidget(btn_cancel)
vlayout.addLayout(hlayout3)
self.setLayout(vlayout)
def show_new_window(self, label):
win = AnotherWindow()
val = win.init_input_dialog()
label.setText(val)
def get_dialog_box_values(self):
check_box_values = [i.text() for i in [self.check_box1, self.check_box2]]
label_values = [i.text() for i in [self.label_value1, self.label_value2]]
print(check_box_values)
print(label_values)
...
...
在for循环中使用
class Window(QWidget):
def __init__(self):
super().__init__()
vlayout = QVBoxLayout(self)
self.check_boxes = []
self.labels = []
for i in range(1, 3):
hlayout = QHBoxLayout()
check_box = QCheckBox(f"label{i}")
button = QPushButton("Set text")
label_value = QLabel("Set value")
button.pressed.connect(lambda x=i-1: self.show_new_window(self.labels[x]))
hlayout.addWidget(check_box)
hlayout.addWidget(button)
hlayout.addWidget(label_value)
self.labels.append(label_value)
self.check_boxes.append(check_box)
# 将当前对象条目添加到主布局
vlayout.addLayout(hlayout)
# 添加按钮:应用和取消
btn_ok = QPushButton("Apply")
btn_ok.clicked.connect(self.get_dialog_box_values)
btn_cancel = QPushButton("Cancel")
btn_cancel.clicked.connect(self.close_dialog_box)
hlayout2 = QHBoxLayout()
hlayout2.addWidget(btn_ok)
hlayout2.addWidget(btn_cancel)
vlayout.addLayout(hlayout2)
def show_new_window(self, label):
win = AnotherWindow()
val = win.init_input_dialog()
label.setText(val)
def get_dialog_box_values(self):
check_box_values = [i.text() for i in self.check_boxes]
label_values = [i.text() for i in self.labels]
print(check_box_values)
print(label_values)
def close_dialog_box(self):
pass
英文:
You need to references to all the widgets that you plan to interact with after they have been created and placed into a layout. In your example and description, at a minimum, you need to be able to interact with the labels at the end of each row, and the checkbox labels, so those are the widgets you should keep a reference to. You can do this by making them attributes of the Window class by giving them the self
assignment. This is an important and one of the key concepts in the world of OOP.
Once they have been assigned as attributes of the window class you can refer to them with the same self
object within any of the instance methods for that class.
For example:
...
...
class Window(QWidget):
def __init__(self):
super().__init__()
vlayout = QVBoxLayout()
hlayout1 = QHBoxLayout()
hlayout2 = QHBoxLayout()
self.check_box1 = QCheckBox("label1")
self.check_box2 = QCheckBox("label2")
button1 = QPushButton("Set text")
button2 = QPushButton("Set text")
self.label_value1 = QLabel("Set value")
self.label_value2 = QLabel("Set value")
button1.pressed.connect(lambda: self.show_new_window(self.label_value1))
button2.pressed.connect(lambda: self.show_new_window(self.label_value2))
hlayout1.addWidget(self.check_box1)
hlayout2.addWidget(self.check_box2)
hlayout1.addWidget(button1)
hlayout2.addWidget(button2)
hlayout1.addWidget(self.label_value1)
hlayout2.addWidget(self.label_value2)
# Add the current objet entry to the main layout
vlayout.addLayout(hlayout1)
vlayout.addLayout(hlayout2)
# Add buttons: Ok and Cancel
btn_ok = QPushButton("Apply")
btn_ok.clicked.connect(self.get_dialog_box_values)
btn_cancel = QPushButton("Cancel")
btn_cancel.clicked.connect(self.close_dialog_box)
hlayout3 = QHBoxLayout()
hlayout3.addWidget(btn_ok)
hlayout3.addWidget(btn_cancel)
vlayout.addLayout(hlayout3)
self.setLayout(vlayout)
def show_new_window(self, label):
win = AnotherWindow()
val = win.init_input_dialog()
label.setText(val)
def get_dialog_box_values(self):
check_box_values = [i.text() for i in [self.check_box1, self.check_box2]]
label_values = [i.text() for i in [self.label_value1, self.label_value2]]
print(check_box_values)
print(label_values)
...
...
Used in a for loop
class Window(QWidget):
def __init__(self):
super().__init__()
vlayout = QVBoxLayout(self)
self.check_boxes = []
self.labels = []
for i in range(1,3):
hlayout = QHBoxLayout()
check_box = QCheckBox(f"label{i}")
button = QPushButton("Set text")
label_value = QLabel("Set value")
button.pressed.connect(lambda x=i-1: self.show_new_window(self.labels[x]))
hlayout.addWidget(check_box)
hlayout.addWidget(button)
hlayout.addWidget(label_value)
self.labels.append(label_value)
self.check_boxes.append(check_box)
# Add the current objet entry to the main layout
vlayout.addLayout(hlayout)
# Add buttons: Ok and Cancel
btn_ok = QPushButton("Apply")
btn_ok.clicked.connect(self.get_dialog_box_values)
btn_cancel = QPushButton("Cancel")
btn_cancel.clicked.connect(self.close_dialog_box)
hlayout2 = QHBoxLayout()
hlayout2.addWidget(btn_ok)
hlayout2.addWidget(btn_cancel)
vlayout.addLayout(hlayout2)
def show_new_window(self, label):
win = AnotherWindow()
val = win.init_input_dialog()
label.setText(val)
def get_dialog_box_values(self):
check_box_values = [i.text() for i in self.check_boxes]
label_values = [i.text() for i in self.labels]
print(check_box_values)
print(label_values)
def close_dialog_box(self):
pass
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论