AttributeError:模块“ tkinter”没有属性“ config”

时间:2018-12-19 19:07:56

标签: python python-3.x tkinter attributeerror

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

 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:模块'tkinter'没有属性'config'

1 个答案:

答案 0 :(得分:0)

在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()

输出:

enter image description here