面向对象的Tkinter编码:更改文本和更新

时间:2016-05-16 03:40:00

标签: python-2.7 oop tkinter

所以我一直在教自己面向Tkinter项目的面向对象编程,因为我清楚地看到它们对于大量编码更加有条理。但是我必须承认,我一直只是从网上复制各种编码,而不是完全理解其目的是什么。

这让我觉得我的代码根本不起作用,我不知道为什么不这样做。第一个问题是简单地改变其他小部件的一个方面的问题。

我有这个示例代码:

    import Tkinter as tk

LARGE_FONT = ("Verdana", 12)

class SeaofBTCapp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        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 = {}
        frame = StartPage(container, self)
        self.frames[StartPage] = frame

        frame.grid(row = 0, column = 0, sticky = "nsew")

        self.show_frame(StartPage)

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

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text = "Start Page", font = LARGE_FONT)
        label.pack(pady = 10, padx = 10)
        button = tk.Button(self, text = "Change Label", command = self.change)
        button.pack(pady = 10, padx = 10)

    def change(self):
        label["text"] = "It has changed"

app = SeaofBTCapp()
app.mainloop()

这应该是一个足够简单的代码,按下按钮,将标签从“起始页”更改为“已更改”。但每当我运行它时,它都表示没有定义全局变量“label”。另外,如果我然后将其更改为self.label,则表明StartPage实例没有属性“label”。我不知道我做错了什么。

此外,以类似的方式,我正在开发一个项目,该项目具有与一个MainApplication类绑定的SideBar类和Main类。 Main类接受一个值并将其显示在Main类的Frame上。在此之后,SideBar中的一个按钮将该值增加1.但主显示不会更新,我不知道如何将主更新与SideBar中的按钮联系起来。

import Tkinter as tk

something = [0, 6]

class Main():
    def __init__(self, root):
        mainboard = tk.Frame(root, height = 100, width = 100)
        self.maincanvas = tk.Canvas(mainboard, bd = 1, bg = "white")

        mainboard.grid(row = 0, column = 0)
        self.maincanvas.grid(row = 0, column = 0)

        self.maincanvas.create_text(45, 50, anchor = "center", text = str(something[1]))

class SideBar():
    def __init__(self, root):
        sidebarframe = tk.Frame(root, height = 100, width = 100)
        button = tk.Button(sidebarframe, width = 20, text = "Change Value", command = self.add)

        sidebarframe.grid(row = 0, column = 1)
        button.grid(row = 0, column = 0)

    def add(self):
        something[1] += 1
        print something[1]


class MainApplication():
    def __init__(self, parent):
        self.parent = parent
        self.sidebar = SideBar(self.parent)
        self.main = Main(self.parent)

if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root)
    root.mainloop()

所有的帮助将不胜感激,但请尽量不要使用很多技术术语,因为我还在学习。

1 个答案:

答案 0 :(得分:0)

在第一个场景中,替换:

label = tk.Label(self, text = "Start Page", font = LARGE_FONT)
label.pack(pady = 10, padx = 10)

使用:

self.label = tk.Label(self, text = "Start Page", font = LARGE_FONT)
self.label.pack(pady = 10, padx = 10)

并且函数change也是这样的:

self.label["text"] = "It has changed"

在你的第二个问题中,我稍微更改了代码以便它可以工作:

import Tkinter as tk

something = [0, 6]

class Main():
    def __init__(self, root):
        mainboard = tk.Frame(root, height = 100, width = 100)
        self.maincanvas = tk.Canvas(mainboard, bd = 1, bg = "white")

        mainboard.grid(row = 0, column = 0)
        self.maincanvas.grid(row = 0, column = 0)

        self.maincanvas.create_text(45, 50, anchor = "center", text = str(something[1]))

class SideBar():
    def __init__(self, root, main):
        self.main = main                                                # Putting the main object to self.main
        sidebarframe = tk.Frame(root, height = 100, width = 100)
        button = tk.Button(sidebarframe, width = 20, text = "Change Value", command = self.add)

        sidebarframe.grid(row = 0, column = 1)
        button.grid(row = 0, column = 0)

    def add(self):
        something[1] += 1
        self.main.maincanvas.delete("all")                                                          # Removing everything from canvas
        self.main.maincanvas.create_text(45, 50, anchor = "center", text = str(something[1]))       # Inserting the new value
        print something[1]


class MainApplication():
    def __init__(self, parent):
        self.parent = parent
        self.main = Main(self.parent)                       # The main needs to run first
        self.sidebar = SideBar(self.parent, self.main)      # So that SideBar can use its canvas

if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root)
    root.mainloop()