tkinter - tk.Stringvar()属性错误

时间:2016-02-22 20:24:16

标签: python oop python-3.x tkinter tk

我的代码如下。我们关注的部分是Page框架中的“labelText = tk.Stringvar()”。

它会抛出以下错误: “AttributeError:module'tkinter'没有属性'Stringvar'”

为什么会抛出错误?我该如何防止此错误?

import tkinter as tk

class Program(tk.Tk):        
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.iconbitmap(self, default = "")
        tk.Tk.wm_title(self, "")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (Page, Other):

            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")

        self.show_frame(Page)

    def show_frame(self,cont):
        frame = self.frames[cont]
        frame.tkraise()


class Page(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        innerFrame = tk.Frame(self, bg="red")
        innerFrame.place(relx=.5, rely=.5, anchor="c", relwidth=1.0, relheight=1.0)

        labelText = tk.Stringvar()

class Other(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

app = Program()
app.state('zoomed')
app.mainloop()

2 个答案:

答案 0 :(得分:2)

Tkinter没有属性Stringvar您应该将其替换为StringVar

labelText = tk.StringVar()

答案 1 :(得分:1)

使用Stringvar()更改StringVar()。 " var"需要资本化。