【问题标题】:Updating text in a QLineEdit text box generated from a for loop reading a dict更新从读取字典的 for 循环生成的 QLineEdit 文本框中的文本
【发布时间】:2019-02-07 07:11:03
【问题描述】:

我有 2 个使用 for 循环生成的 QLineEdit 框,它们从字典中提取名称和初始文本值。我拥有的代码可以很好地更新字典值,并正确生成编辑框。我遇到的问题实际上是更新 gui 上的值以供用户查看。我在 windows 10 pro 上使用 pyside2 和 python 3.7.2。

如果我对 QLineEdit 框进行“硬编码”,则使用 self.display1.setText("asdf") 可以按预期工作。我生成输入的方式除了更新分配给编辑框文本的字典值之外,我无法弄清楚如何做到这一点。我尝试在单击按钮时以及修改字典中的值后调用update()repaint()。我还尝试通过调用重新制作输入并执行.addWidget() 的函数来重新定义小部件。正如预期的那样,这在已经生成的编辑框下方生成了新的。但是,框中的文字是正确的。

这是生成输入的循环

for val, key in self.inputDict.items():
    self.inputs = QLineEdit(key, self)
    self.inputs.setText(key)
    self.vertCol.addWidget(self.inputs)

相关字典:self.inputDict = {"display1": "", "display2": ""}.

这是生成按钮的循环(它在一个循环中,因为这只是我正在构建的这个程序的一个功能,其他按钮滚动不同面的骰子)

for key, val in self.buttonDict.items():
    self.buttons = QPushButton(key, self)
    self.buttons.setToolTip("D20 die rolled: 1-10=low, 11-20=high; Coin Flip: 0=Heads(Good) 1=Tails(Bad)")
    self.buttons.clicked.connect(partial(self.handlehlgb))
    self.vertCol.addWidget(self.buttons)

相关按钮字典self.buttonDict = {"High-Low-Good-Bad": "hlgb"}

这是我要开始工作的功能:

    def handlehlgb(self):
        self.coinFlip = randrange(2)
        self.rollD20 = randint(1, 20)
        if self.coinFlip <= 0:
            self.inputDict.update({'display1': 'Good'})
            print(self.inputDict["display1"])
        else:
            self.inputDict.update({'display2': 'Bad'})
            print(self.inputDict["display2"])
        if self.rollD20 <= 10:
            self.inputDict.update({'display2': 'Low'})
            print(self.inputDict["display2"])
        else:
            self.inputDict.update({'display2': 'High'})
            print(self.inputDict["display2"])

它的目的是显示根据“掷硬币”确定的好/坏以及根据 D20 掷骰子确定的高/低。

我希望它能更新 GUI 上的显示,但事实并非如此。它将打印控制台中显示的正确值。这是我正在使用的粘贴箱:https://pastebin.com/H6LAnbnH

我对 python 比较陌生,尤其是 pyside/pyqt。因此,任何帮助将不胜感激!

【问题讨论】:

    标签: python-3.x pyqt5 pyside2


    【解决方案1】:

    我可能理解错了,但是如果你在掷骰子后简单地添加一个setText:

    self.inputDict.update({'display1': 'Good'})
    self.inputs.setText(self.inputDict["display2"])
    

    文本在 QLineEdit 中更新。

    那么你可能想把self.inputs 做成一个字典,这样你就可以改变两个QLineEdits:

    self.inputs = {}
    for val, key in self.inputDict.items():
        self.inputs[key] = QLineEdit(key, self)
    
    ...
    
    self.inputDict.update({'display1': 'Good'})
    self.inputs['display1'].setText(self.inputDict['display1'])
    

    (等等……现在我复制粘贴了你的代码,我看到你写了val,key in ...,而你可能希望它是key,val in ...)。难道就这么简单吗?

    [编辑] 这两种想法都会变成这样:

    class MainWidget(QWidget):
        def __init__(self):
            super(MainWidget, self).__init__()
            self.buttonDict = {"High-Low-Good-Bad": "hlgb"}
            self.inputDict = {"display1": "", "display2": ""}
            self.vertCol = QVBoxLayout(self)
    
            self.inputs = {}
            for key, val in self.inputDict.items():
                self.inputs[key] = QLineEdit(key, self)
                self.inputs[key].setText(val)
                print("Key - " + key)
                print("Val - " + val)
                self.vertCol.addWidget(self.inputs[key])
    
            for key, val in self.buttonDict.items():
                self.buttons = QPushButton(key, self)
                self.buttons.setToolTip("D20 die rolled: 1-10=low, 11-20=high; Coin Flip: 0=Heads(Good) 1=Tails(Bad)")
                self.buttons.clicked.connect(partial(self.handlehlgb))
                #self.buttons.clicked.connect(self.inputs.update())
                self.vertCol.addWidget(self.buttons)
    
        def handlehlgb(self):
            self.coinFlip = randrange(2)
            self.rollD20 = randint(1, 20)
            if self.coinFlip <= 0:
                self.inputDict.update({'display1': 'Good'})
                self.inputs['display1'].setText('Good')
                print(self.inputDict["display1"])
            else:
                self.inputDict.update({'display2': 'Bad'})
                self.inputs['display1'].setText('Bad')
                print(self.inputDict["display2"])
            if self.rollD20 <= 10:
                self.inputDict.update({'display2': 'Low'})
                self.inputs['display2'].setText('Low')
                print(self.inputDict["display2"])
            else:
                self.inputDict.update({'display2': 'High'})
                self.inputs['display2'].setText('High')
                print(self.inputDict["display2"])
    
    

    【讨论】:

    • self.inputs['display1'].setText(self.inputDict['display1']) 返回一个 TypeError TypeError: 'PySide2.QtWidgets.QLineEdit' object is not subscriptable 所以我厌倦了没有第一组括号,它现在更新了 gui,只更新了底部的文本区域。我也改变了 key 和 val,同样的行为。
    • 使输入成为字典会导致此错误:AttributeError: 'dict' object has no attribute 'setText'
    • 非常感谢!我不知道在修改您的建议时我错过了什么,但是您放在那里的内容 100% 有效!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多