我想创建一个窗口,它从用户那里获取输入,点击显示后,它会输出输入的内容:
import sys
import tkinter as tk
import time
class Application(tk.Frame):
def __init__(self):
super().__init__()
self.pack()
self.create_widgets()
def create_widgets(self):
self.hi_there = tk.Button(self,text = "show",command =
self.Show_enter,fg="red",bg="blue")
self.hi_there.pack(side = "right")
self.UN = tk.Label(text="User Name").pack(side="left")
self.enter = tk.Entry(bd=5, bg="red").pack(side="right")
def Show_enter(self):
s = self.enter.get()
print(s)
root = tk.Tk()
app = Application()
app.master.title("coool")
app.mainloop()
然而,我收到错误:
AttributeError:'NoneType'对象没有属性'get'
答案 0 :(得分:0)
不要直接使用.pack()。以下是如何执行此操作的示例:
self.entry = Entry(root, width=25)
self.entry.pack()
def get_value(self):
val = self.entry.get()
print(val)