从其他类

时间:2018-06-11 16:12:08

标签: python tkinter

我正在尝试编写可以从写入的类之外的类引用tkinter对象的函数。我的class Base代码来自Youtube上“Sentdex”的tkinter教程,下面的页面是他的技术的简单迭代。

我希望能够在点击其他页面上的按钮后从ButtonOne PageOne DISABLED重新配置NORMAL,但无法找到任何工作方法在线。

import tkinter as tk
from tkinter import *

class Base(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 = {}
        for F in (PageOne, PageTwo, PageThree):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(PageOne)

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

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.config(height=500, width=1500, bg="Red")
        self.Button1 = tk.Button(self, text="Button 1", state=DISABLED)
        self.Button1.pack()
class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.config(height=500, width=1500, bg="Blue")
        self.Button2 = tk.Button(self, text="Button 2", state=NORMAL)
        self.Button2.pack()

class PageThree(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        menu = tk.Menu(self)
        controller.config(menu=menu)
        self.config(height=500, width=1500, bg="Green")
        menu = tk.Menu(self)
        controller.config(menu=menu)
        subMenu = tk.Menu(self)
        menu.add_cascade(label="Pages", menu=subMenu)
        subMenu.add_command(label="Page One", command=lambda: controller.show_frame(PageOne))
        subMenu.add_command(label="Page Two", command=lambda: controller.show_frame(PageTwo))
        subMenu.add_command(label="Page Three", command=lambda: controller.show_frame(PageThree))
        self.Button3 = tk.Button(self, text="Button 3", state=NORMAL)
        self.Button3.pack()

program = Base()
program.mainloop()

当我尝试类似

self.Button3 = tk.Button(self, text="Button 3", state=NORMAL, command=self.enableOne)
    self.Button3.pack()

def enableOne(self):
    Base.PageOne.Button1.config(state=NORMAL)

我收到错误消息:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:/Users/R21660/PycharmProjects/Step1/ExampleforStack.py", line 53, in enableOne
    Base.PageOne.Button1.config(state=NORMAL)
AttributeError: type object 'Base' has no attribute 'PageOne'

感谢。

更新: self.Button3 = tk.Button(self, text="Button 3", state=NORMAL, command=lambda: PageOne.Button1.config(state=NORMAL))不起作用,因为“PageOne”没有属性“Button1。”

但是,command=lambda: PageOne.enableOne)什么都不做,但没有给出错误信息。

1 个答案:

答案 0 :(得分:1)

Base类不知道PageOne实例,但Base实例(内部帧列表)知道它。

而不是:

def enableOne(self):
    Base.PageOne.Button1.config(state=NORMAL)

我会尝试:

def enableOne(self):
    self.controller.frames[PageOne].Button1.config(state=NORMAL)

前提是您在Page3

init 中添加了self.controller = controller