簡體   English   中英

Tkinter-AttributeError:“ str”對象沒有屬性“ set”

[英]Tkinter - AttributeError: 'str' object has no attribute 'set'

這是我創建並嘗試運行的代碼:

import tkinter as tk


def ok(val):
    print("Value is: ", val)

def say_hi(self):
    print("hi there, everyone!")

class Application(tk.Frame):

    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        master.title("Hi There")
        master.geometry("400x400")
        self.createWidgets(master)

    def createWidgets(self, master=None):
        var = str()

        self.select = tk.OptionMenu(master, var, "one", "two","three", command=ok).grid(column=1, row=1)

        self.QUIT = tk.Button(master, text="QUIT", fg="red", command=root.destroy).grid(column=2, row=1)
        print ("HI")

root = tk.Tk()
app = Application(master=root)
app.mainloop()

但是我收到以下錯誤:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
  File "C:\Python34\lib\tkinter\__init__.py", line 3308, in __call__
    self.__var.set(self.__value)
AttributeError: 'str' object has no attribute 'set'

我嘗試弄亂一些變量並使用不同的方法來使菜單正常工作,但是似乎沒有什么可以消除錯誤。

關於如何解決錯誤的任何想法?

使用StringVar而不是str

  def createWidgets(self, master=None):
        var = tk.StringVar()

python str沒有set方法或屬性,StringVar特定於tkinter以及您打算使用的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM