<分区> 分区>
我仍然没有完全理解类的范围。我已经阅读了这篇文章,并且我一直在尝试做不同的事情来取得一些成功,但是这个让我摸不着头脑。我正在玩弄这段代码。我认为 Bryan Oakley 可能已经写了这个例子,但不是正面的。
基本上,它是一个分页示例。我添加了 buttoncontroloff 和 buttoncontrolon 函数来尝试控制菜单按钮的状态。我在 Page1 类中试过这个,但没有用。
class Page1(Page):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, MainMenu, *args, **kwargs)
newframe = tk.Frame(self, width=780, height=540, background="red")
newframe.place(anchor="c", relx=.5, rely=.5)
main = MainView()
main.buttoncontrolon()
我在开始时确实禁用了其中一个按钮,所以我有一部分。但是当我试图在两个类(class)之间这样做时,我无法弄清楚。当您单击第 2 页按钮时,我只想启用第 1 页按钮。
import Tkinter as tk
from Tkinter import *
class Page(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
def show(self):
self.lift()
class Page1(Page):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
newframe = tk.Frame(self, width=780, height=540, background="red")
newframe.place(anchor="c", relx=.5, rely=.5)
class Page2(Page):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
newframe = tk.Frame(self, width=780, height=540, background="blue")
newframe.place(anchor="c", relx=.5, rely=.5)
class MainView(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
p1 = Page1(self)
p2 = Page2(self)
buttonframe = tk.Frame(self)
container = tk.Frame(self)
buttonframe.pack(side="top", fill="x", expand=False)
container.pack(side="top", fill="both", expand=True)
p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
self.b1 = tk.Button(buttonframe, text="Page 1", command=lambda: p1.lift())
self.b1.pack(side="left")
self.b2 = tk.Button(buttonframe, text="Page 2", command=lambda: p2.lift())
self.b2.pack(side="left")
p1.show()
def buttoncontroloff(self):
self.b1.config(state = DISABLED)
def buttoncontrolon(self):
self.b1.config(state = NORMAL)
if __name__ == "__main__":
root = tk.Tk()
main = MainView(root)
main.buttoncontroloff()
main.pack(side="top", fill="both", expand=True)
root.wm_geometry("800x600")
root.mainloop()
