【问题标题】:Error with Switching frames in Tkinter在 Tkinter 中切换帧时出错
【发布时间】:2018-10-06 12:09:26
【问题描述】:

我正在尝试将 python GUI 作为学校课程的一部分,我正在尝试使用一个按钮来切换框架,该按钮设置为破坏旧框架并打开新框架,而我在打开 a 时没有任何问题新框架我似乎无法破坏旧框架或将任何小部件等加载到新框架中。这是我遇到问题的代码部分:

class welcomePage(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller

    homebutton = tk.Button(self, text = "Return Home", command=lambda: self.home(), width = 30, height = 1)
    homebutton.pack(padx = 10, pady= 20 )

    cswitch = tk.Button(self, text = "Customer Interface", command=lambda: (self.destroy, CustomerPage()), width = 30, height = 1)
    cswitch.pack(padx = 10, pady= 15 )

    exit = tk.Button(self, text="Exit", command=self.destroy, width = 30, height = 1)
    exit.pack(padx = 10, pady= 20 )

class CustomerPage(tk.Tk):
def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)


    self.title("Customer Interface")
    self.minsize(width = 300, height = 100)
    self.maxsize(width = 9999, height = 9999)
    tab_control = tk.Notebook(self)
    admin1 = tk.Frame(tab_control)
    admin2 = tk.Frame(tab_control)
    admin3 = tk.Frame(tab_control)
    admin4 = tk.Frame(tab_control)
    tab_control.add(admin1, text='Menu')
    tab_control.add(admin2, text='DataBase')
    tab_control.add(admin3, text='Sales History')
    tab_control.add(admin4, text='Search')        

                # -- Menu -- #

    homebutton = tk.Button(admin1, text = "Return Home", command=lambda: self.home(), width = 30, height = 1)
    homebutton.pack(padx = 10, pady= 30 )

    cswitch = tk.Button(admin1, text = "Admin Interface", command=lambda: self.cswitch(), width = 30, height = 1)
    cswitch.pack(padx = 10, pady= 10 )

    exit = tk.Button(admin1, text="Exit", command=self.destroy, width = 30, height = 1)
    exit.pack(padx = 10, pady= 30 )

这是我得到的错误:AttributeError: module 'tkinter' has no attribute 'Notebook'

我无法在网上找到有关此特定错误的任何信息,因此非常感谢任何帮助:)。

【问题讨论】:

  • 我认为您正在寻找ttk.Notebook。 (import tkinter.ttk as ttk)
  • @Aran-Fey 这给了我这个错误: AttributeError: type object 'Notebook' has no attribute 'tk' - 我尝试将所有 tk.Frames 更改为 ttk.Frame 但错误仍然存​​在一样的。
  • 请创建一个minimal reproducible example。您的代码非常混乱。一个“页面”是根窗口,一个“页面”是一个框架。目前尚不清楚这是否是故意的。另外,您没有创建这些类中的任何一个的实例。此外,正如 Aran-Fey 所提到的,错误是正确的:tkinter 没有笔记本。您需要使用 ttk 包中的 Notebook。最后,请修复代码中的缩进问题。

标签: python python-3.x tkinter


【解决方案1】:

这应该可行:

from tkinter import ttk

tab_control = ttk.Notebook(self)

【讨论】:

    猜你喜欢
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2018-07-17
    • 2020-10-10
    相关资源
    最近更新 更多