我仍然没有完全理解课程的范围。我已经阅读了这篇文章并且我一直试图做一些不同的事情来取得一些成功,但是这个让我摸不着头脑。我正在玩这个代码。我认为布莱恩奥克利可能已经写了这个例子,但并不积极。
基本上,它是一个分页示例。我添加了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()
我在启动时禁用其中一个按钮,所以我有一部分。但是当我在两个班级之间尝试这样做时,我无法弄明白。当您单击第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()
答案 0 :(得分:1)
我这样做:在MainView
中编写一个方法来打开编号页面(0 ...):
def open_page(self, page_number):
# enable all except the button for this page.
for i, button in enumerate(self.buttons):
if i == page_number:
button.config(state=DISABLED)
else:
button.config(state=ACTIVE)
self.pages[page_number].lift()
使按钮使用此方法传递页码,作为命令:
self.b1 = tk.Button(buttonframe,
text="Page 1", command=lambda: self.open_page(0))
self.b2 = tk.Button(buttonframe,
text="Page 2", command=lambda: self.open_page(1))
然后让所有页面和按钮进入这些列表:
p1 = Page1(self)
p2 = Page2(self)
self.pages = [ p1, p2 ]
和
self.buttons = [ self.b1, self.b2 ]
请记住,列表使用基于0的索引。现在,稍后可以轻松地重构为add_page
创建页面按钮,并将两者放入列表等。
Page
不需要关注Button
自己;这不是他们的责任范围,Button
不属于他们。
答案 1 :(得分:0)
Antti,我修正了错误。以下代码效果很好!我会将您的解决方案标记为已回答。再次感谢。
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")
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)
self.pages = [ p1, p2 ]
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: self.open_page(0))
self.b2 = tk.Button(buttonframe, text="Page 2", command=lambda: self.open_page(1))
self.b2.pack(side="left")
self.b1.pack(side="left")
self.buttons = [ self.b1, self.b2 ]
p1.show()
def open_page(self, page_number):
# enable all except the button for this page.
for i, button in enumerate(self.page_buttons):
if i == page_number:
button.config(state=DISABLED)
else:
button.config(state=ACTIVE)
self.pages[page_number].lift()
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()