问题描述
限时送ChatGPT账号..所以我用文件选择"和去"按钮启动一个根屏幕.转到按钮已禁用,我想在选择文件后将其激活.当我选择文件时,go 应该变为活动状态,而是出现此错误TypeError:'NoneType' 对象不支持项目分配"这是一些示例代码
So I start a root screen with a "file select" and a "go" button. The go button is disabled and I want to make it active after the file has been selected. When I select the file go should become active but instead this error "TypeError: 'NoneType' object does not support item assignment" Here is some sample code
import Tkinter
import tkFileDialog
def chooseDir():
dir1=tkFileDialog.askopenfilename(parent=root, title='choose file path')
go['state']=Tkinter.ACTIVE
root=Tkinter.Tk()
global go
go=Tkinter.Button(text='file location',command=chooseDir,state=Tkinter.DISABLED).pack()
root.mainloop()
推荐答案
这一行:
go=Tkinter.Button(text='file location',command=chooseDir,state=Tkinter.DISABLED).pack()
正在创建一个临时对象,然后对其调用 pack().pack 方法返回 None,所以 go
被分配了 None.
is creating a temporary object, then calling pack() on it. The pack method returns None, so go
is assigned None.
删除 .pack() 然后 go
将成为 Button 对象.然后调用go.pack()
.
Remove the .pack() then go
will be the Button object. Then call go.pack()
.
这篇关于类型错误:'NoneType'对象不支持项目分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论