相关疑难解决方法(0)

Tkinter:AttributeError:NoneType对象没有属性get

我在类似的错误消息上看到了其他一些帖子,但找不到可以解决我的问题的解决方案.

我用TkInter稍微涉足并创建了一个非常简单的UI.该守则如下─

from tkinter import *

root = Tk()

def grabText(event):
    print(entryBox.get())    

entryBox = Entry(root, width=60).grid(row=2, column=1, sticky=W)

grabBtn = Button(root, text="Grab")
grabBtn.grid(row=8, column=1)
grabBtn.bind('<Button-1>', grabText)

root.mainloop()
Run Code Online (Sandbox Code Playgroud)

我启动并运行UI.当我单击Grab按钮时,我在控制台上收到以下错误:

C:\Python> python.exe myFiles\testBed.py
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python\lib\lib-tk\Tkinter.py", line 1403, in __call__
    return self.func(*args)
  File "myFiles\testBed.py", line 10, in grabText
    if entryBox.get().strip()=="":
AttributeError: 'NoneType' object has no attribute 'get'
Run Code Online (Sandbox Code Playgroud)

错误追溯到entryBox.

我敢肯定有人可能以前处理过这个问题.任何帮助表示赞赏.

python user-interface tkinter

38
推荐指数
3
解决办法
7万
查看次数

tkinter 中无法设置背景颜色

我对这个简单的 tkinter 程序越来越绝望:我似乎无法更改背景颜色(或单个小部件的颜色)!这里出了什么问题?

以下是我的各种尝试以及收到的错误的要点

import tkinter
import tkinter.ttk as tk

root = tkinter.Tk()

frame= tk.Frame(root)
frame.grid(column=0, row=0)

tk.Button(frame, text="Open file", command=None).grid(column=0, row=1 )
lab=tk.Label(frame, text="test test test test test test ").grid(column=0, row=2 )

#root.config(background="black")    # does nothing
#frame.config(background="black")   # Error: unknown option "-background"
#lab.config(background="black")     # Error: 'NoneType' object has no attribute 'config'

root.mainloop()
Run Code Online (Sandbox Code Playgroud)

python tkinter colors

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×2

tkinter ×2

colors ×1

user-interface ×1