Python 如何更改Tkinter中级联项的命令
我已经在Python 如何更改Tkinter中级联项的命令,python,tkinter,Python,Tkinter,我已经在self.init_window()的文件下创建了一个级联项backbutton,但是当我尝试在self.writeWindow()中更改此按钮的命令时,我得到了一个错误“NoneType”对象没有属性“configure”。我做错了什么?谢谢 #import tkinter libs from python from tkinter import * import os #main class class Window(Frame): #__init__ = i
self.init_window()
的文件下创建了一个级联项backbutton
,但是当我尝试在self.writeWindow()中更改此按钮的命令时,我得到了一个错误
“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()
add_命令
不返回可配置的对象。如果要重新配置菜单项,必须使用entryconfigure
,并且必须在菜单上调用它,为其提供要配置的项的索引。索引可以是数字或项目标签(或其他一些内容)
例如:
self.fileMenu = Menu(...)
self.fileMenu.add_command(label="Back", ...)
...
self.fileMenu.entryconfigure("Back", command=...)
请阅读以下内容:如果您能将代码精简到最低限度,将更容易帮助您。请阅读本指南: