如何在Tkinter中更改级联项的命令

时间:2015-12-11 19:02:37

标签: python tkinter

我在backbutton中的文件下创建了一个级联项self.init_window()但是当我尝试在self.writeWindow() I get the error中更改此按钮的命令时'NoneType'对象没有属性'configure' `。我究竟做错了什么?感谢

#import tkinter libs from python
from tkinter import *
import os       
#main class
class Window(Frame): 
    #__init__ = initilise (run straigh away)
    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.master = master
        self.init_window()
    def init_window(self):
        #define global variables  
        global backbutton  
        #main window title
        self.master.title("") 
        self.pack(fill=BOTH, expand=1) 
        #create a menu         
        menu = Menu(self.master)
        self.master.config(menu=menu)        
        #create file icon on casacde        
        file=Menu(menu)                
        #add dropdown items               
        menu.add_cascade(label="File", menu=file)        
        #create help icon on cascade                               
        backbutton = file.add_command(label="Back", command = self.backCommand)                                
        #creating buttons
        #the read buttons calls the method readCommand      
        buttonwrite = Button(self, text="Write File", command=self.writeWindow)                
        #placing the buttons in place on the GUI      
        buttonwrite.grid(row=2, column=1)                       
        #adding a label
        question = Label(self, text="Please Select an Option: ", font=("Helvetica", 16))
        question.grid(row=1, column=1)                         
    def writeWindow(self):                                    
        #This is not working -- change the command of the backbutton dropdown        
        backbutton.configure(0, command = self.backCommand1)                
        #Changing the name of the window, to become releveant
        self.master.title("Write Files")
        self.pack(fill=BOTH, expand=1)                                                                              
    def backCommand(self):
      print("")                   
    def backCommand1(self):        
       print("")                              
#base geometry for the main window
root = Tk()
root.geometry("400x100")
app = Window(root)
root.mainloop()

1 个答案:

答案 0 :(得分:0)

add_command没有返回可配置的对象。如果要重新配置菜单项,必须使用entryconfigure,并且必须在菜单上调用它,为其指定要配置的项目的索引。索引可以是数字或项目标签(或其他一些东西)。

例如:

self.fileMenu = Menu(...)
self.fileMenu.add_command(label="Back", ...)
...
self.fileMenu.entryconfigure("Back", command=...)