【发布时间】:2019-10-19 16:02:39
【问题描述】:
我正在尝试从作为我的输入的输入框中获取一个值,并且我希望其中写入的任何内容都可用于在按下按钮时进行处理。这是我的代码:
from tkinter import *
import tkinter as tk
class GUI(Tk):
def __init__(self):
super(GUI, self).__init__()
tk.Label(self, text="Input").grid(row=0)
self.input = tk.Entry(self).grid(row=0, column=1)
tk.Button(self, text="Start", command=self.start_button).grid(row=5, column=1)
def start_button(self):
print(self.input.get())
gui = GUI()
gui.mainloop()
每当我尝试按下按钮时,它都会显示 None 类型没有属性“get”。这是回溯:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files (x86)\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "D:/Projects/excel_reader/test.py", line 15, in start_button
print(self.input.get())
AttributeError: 'NoneType' object has no attribute 'get'
我尝试将 TK 作为我的 GUI 类的子类删除,删除 super() 调用并尝试全局声明所有变量,但这也不起作用。我对 Python GUI 很陌生。
【问题讨论】:
标签: python tkinter tkinter-entry