首页 文章

AttributeError:'str'对象在python pyQt [duplicate]中没有属性'set'错误

提问于
浏览
-1

这个问题在这里已有答案:

我有这个代码:

self.entriesx = self.generate_stringvars()

# Fill in the entry fields if a template is selected.
def autofill(self):
    # Generate a dictionary of all the saved templates.
    test_dictionary = self.build_test_dictionary()
    # Retrive the selected qual name from the form.
    self.coded_entry = self.comboBox.currentText()
    entries = test_dictionary[self.coded_entry]
    # Set each entry field in self.entriesx to the corresponding value
    # from the template.
    i_count = 0
    for i in self.entriesx:
        i.set(entries[i_count])
        i_count += 1
    # Reload the entry fields with their new values.
    self.add_entry_fields()

autofill(self) 函数根据组合框中的选定项自动将相应条目填入输入字段 . 自动填充功能还依赖于此功能:

# Generate a list of string variables to store the entries.
def generate_stringvars(self):
    temp_entriesx = []
    count = 0
    while count < 21:
        temp_entriesx.append("")
        count += 1
    return temp_entriesx

当我编译我的代码时,我的自动填充功能中出现 i.set(entries[i_count]) 错误 i.set(entries[i_count]) . 我如何解决它?

Edit: 这是this帖子的后续问题 .

1 回答

  • 0

    对于困惑,这是this question的后续行动 . OP正试图从Tkinter移植到pyQt .

    Tkinter的 StringVar 具有与python str 不同的接口 . 使用赋值运算符( = )完成赋值 . 此外,作为一般规则,尽量不要将 i 或类似用于非整数类型 . 这没错,只是糟糕的编码习惯 . 此外,因为您不再需要元素本身 . 只需使用 range .

    for i in range(len(self.entriesx)):
        self.entriesx[i] = entries[i]
    

相关问题