【问题标题】:How to convert stringVar() from tk to pyqt如何将 stringVar() 从 tk 转换为 pyqt
【发布时间】:2018-06-20 23:48:23
【问题描述】:

我有这个函数作为我的代码的一部分,我正在尝试从 tk 转移到 pyqt,但我在 pyqt 方面没有太多经验。我正在尝试生成一个 Tkinter 字符串变量列表来存储条目。

def generate_stringvars(self):
    temp_entriesx = []
    count = 0
    while count < 21:
        e = tk.StringVar()
        e.set("")
        temp_entriesx.append(e)
        count += 1
    return temp_entriesx

如何更改 tk.StringVar() 以使其在 pyqt 中工作?

谢谢

【问题讨论】:

    标签: python tkinter pyqt


    【解决方案1】:

    pyQt 和 Qt 之间存在一些差异。一个重要的区别是没有明确需要 QString (see here)。 pyQt 完全兼容 Python 内置的字符串类型。您的代码将如下所示。

    def generate_stringvars(self):
        temp_entriesx = []
        count = 0
        while count < 21:
            temp_entriesx.append("")
            count += 1
        return temp_entriesx
    

    【讨论】:

    • 我得到一个错误,AttributeError: 'str' object has no attribute 'set',对于这一行,i.set(entries[i_count]) 。你知道我该如何解决吗? @buck54321
    • @prexos 看到这是一个单独的问题,您应该提出一个新问题。没有人可以在没有看到代码的情况下回答您的评论问题。也就是说,错误告诉您i 是一个字符串对象,而不是您的StringVar(),听起来您认为它是。
    • @Mike-SMT 感谢您的回复。一旦时间限制过去,我会发布我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多