仍在与 class 范围作斗争
Still struggling with class scope
我还没有完全理解 classes 的范围。我已经阅读了这篇文章,并且我一直在尝试做不同的事情来取得一些成功,但是这个让我摸不着头脑。我正在玩弄这段代码。我认为 Bryan Oakley 可能已经写了这个例子,但不是正面的。
基本上就是一个分页的例子。我添加了 buttoncontroloff 和 buttoncontrolon 函数来尝试控制菜单按钮的状态。我在第 1 页 class 中尝试过,但没有用。
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()
我会这样做:在 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
不属于他们。
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()
我还没有完全理解 classes 的范围。我已经阅读了这篇文章,并且我一直在尝试做不同的事情来取得一些成功,但是这个让我摸不着头脑。我正在玩弄这段代码。我认为 Bryan Oakley 可能已经写了这个例子,但不是正面的。
基本上就是一个分页的例子。我添加了 buttoncontroloff 和 buttoncontrolon 函数来尝试控制菜单按钮的状态。我在第 1 页 class 中尝试过,但没有用。
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()
我会这样做:在 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
不属于他们。
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()