【问题标题】:AttributeError: module 'tkinter' has no attribute 'config'AttributeError:模块 'tkinter' 没有属性 'config'
【发布时间】:2024-04-20 18:05:01
【问题描述】:

我正在尝试制作一个简单的菜单(目前),但我不断收到此错误:

 class pyFinanceStart(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 (StartPage):

             frame = F(container, self)
             self.frames[F] = frame
             frame.grid(row=0, column=0, sticky="nsew")

             self.show_frame(StartPage)

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


 class StartPage(tk.Frame):

     def __init__(self, parent, controller):
          tk.Frame.__init__(self,parent)                
          container = tk.Frame(self)
          container.pack(side="top", fill="both", expand = True)
          container.grid_rowconfigure(0, weight=1)
          container.grid_columnconfigure(0, weight=1)

          menubar = tk.Menu(container)        
          topIndi = tk.Menu(menubar, tearoff=1)
          topIndi.add_command(label="None")#,command=lambda: addTopIndicator('none'))
          topIndi.add_separator()
          topIndi.add_command ( label="RSI")#,command=lambda: addTopIndicator('rsi'))
          topIndi.add_command ( label="MACD")#,command=lambda: addTopIndicator('macd'))
          menubar.add_cascade(label = "Top Indicator", menu = topIndi)

          helpmenu = tk.Menu(menubar, tearoff=0)
          helpmenu.add_command(label="Help")
          menubar.add_cascade(label="Help", menu=helpmenu)

          tk.Tk.config(self, menu=menubar)

我有错误:AttributeError: module 'tkinter' has no attribute 'config'

【问题讨论】:

  • 不是我在使用 python 3.x
  • 请发布堆栈跟踪以及任何其他有助于调试问题的信息。
  • 您需要在问题中包含足够多的代码,以便其他人重现该问题。请edit 这样做。当我尝试按照当前发布的方式运行您的代码时,我在 tk.Tk.config(self, menu=menubar) 行上收到另一个错误——这确实是错误的。

标签: python python-3.x tkinter attributeerror


【解决方案1】:

在我的 Windows 框中使用 Python 3.7.1 报告的错误与您的不同。我的是_tkinter.TclError: unknown option "-menu"。这是因为menu 选项仅受*容器支持,Frame 小部件不支持。

以下是根据您的修改的代码来修复错误:

import tkinter as tk

class pyFinanceStart(tk.Tk):
  def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)
    self.geometry("300x100")
    # make the frame using all the window area
    self.rowconfigure(0, weight=1)
    self.columnconfigure(0, weight=1)
    # create the pages
    self.frames = {}
    for F in (StartPage,):
      frame = F(self)
      frame.grid(row=0, column=0, sticky="nsew")
      self.frames[F] = frame
    # show the start page
    self.show_frame(StartPage)

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

class StartPage(tk.Frame):
  def __init__(self, parent):
    tk.Frame.__init__(self, parent, bg='blue')
    tk.Label(self, text="Start Page", bg='#ffc').pack(fill='both', expand=1, padx=10, pady=10)
    # create the menubar
    menubar = tk.Menu(parent)
    # create the 'Top Indicator' menu
    topIndi = tk.Menu(menubar, tearoff=1)
    topIndi.add_command(label="None")
    topIndi.add_separator()
    topIndi.add_command(label="RSI")
    topIndi.add_command(label="MACD")
    menubar.add_cascade(label="Top Indicator", menu=topIndi)
    # create the 'Help' menu
    helpmenu = tk.Menu(menubar, tearoff=0)
    helpmenu.add_command(label="Help")
    menubar.add_cascade(label="Help", menu=helpmenu)
    # set the toplevel menubar
    parent.config(menu=menubar) # or tk.Tk.config(parent, menu=menubar)

pyFinanceStart().mainloop()

还有输出:

【讨论】: