Python 如何制作tkinter输入框并将其关闭?
蟒蛇 我无法将整个问题放在标题中,因此它是: 我试图创建一个tkinter框,它接受输入并在接受输入后关闭,这可以简单地通过root.destroy()命令完成,但这不是问题所在。我试图通过我的代码来访问输入(用户提供的)。每当我尝试使用return语句时,它都不会被注意到,因为root.destroy()命令会在它之前使用它。同样的事情,反之亦然Python 如何制作tkinter输入框并将其关闭?,python,tkinter,Python,Tkinter,蟒蛇 我无法将整个问题放在标题中,因此它是: 我试图创建一个tkinter框,它接受输入并在接受输入后关闭,这可以简单地通过root.destroy()命令完成,但这不是问题所在。我试图通过我的代码来访问输入(用户提供的)。每当我尝试使用return语句时,它都不会被注意到,因为root.destroy()命令会在它之前使用它。同样的事情,反之亦然 from tkinter import * score = 0 root = Tk() nameLabel = Label(root, text="
from tkinter import *
score = 0
root = Tk()
nameLabel = Label(root, text="Name")
ent = Entry(root, bd=5)
def getName():
global score
entt= (ent.get())
score = 1
root.destroy()
return entt
b1 = Button(root, text='FirstC', command=getName)
b1.pack(side=LEFT, padx=5, pady=15)
nameLabel.pack()
ent.pack()
root.mainloop()
print(func1())
错误
回溯(最近一次呼叫最后一次):
文件“C:\Users\14753\OneDrive\Desktop\Stocks\trying.py”,第21行,在
打印(getName())
文件“C:\Users\14753\OneDrive\Desktop\Stocks\trying.py”,第9行,在getName中
entt=(ent.get())
文件“C:\Users\14753\AppData\Local\Programs\Python\Python37-32\lib\tkinter\\uuuuu init\uuuuu.py”,第2682行,在get中
返回self.tk.call(self.\w'get')
_tkinter.TclError:无效的命令名“!条目”
我已经研究这个问题有一段时间了,现在任何帮助都会非常有用
按钮
运行函数,但它没有获取返回值的方法。在函数中,您必须将条目中的文本分配给全局变量,然后在函数外部使用此变量
import tkinter as tk
# --- functions ---
def get_name():
global name # inform function to use global variable instead of creating local one
name = name_entry.get() # assign text to global variable
root.destroy()
# --- main ---
name = '' # global variable with default value (if you don't put name)
root = tk.Tk()
name_label = tk.Label(root, text='Name')
name_label.pack()
name_entry = tk.Entry(root)
name_entry.pack(side='right')
b = tk.Button(root, text='First', command=get_name)
b.pack(side='left')
root.mainloop()
print('name:', name) # display text from global variable
按钮
运行函数,但它没有获取返回值的方法。在函数中,您必须将条目中的文本分配给全局变量,然后在函数外部使用此变量
import tkinter as tk
# --- functions ---
def get_name():
global name # inform function to use global variable instead of creating local one
name = name_entry.get() # assign text to global variable
root.destroy()
# --- main ---
name = '' # global variable with default value (if you don't put name)
root = tk.Tk()
name_label = tk.Label(root, text='Name')
name_label.pack()
name_entry = tk.Entry(root)
name_entry.pack(side='right')
b = tk.Button(root, text='First', command=get_name)
b.pack(side='left')
root.mainloop()
print('name:', name) # display text from global variable
什么是func1
?代码对我来说工作正常。除了它找不到的func1
。return entt
未被注意到,因为按钮运行此函数,并且它没有获取此值的方法。您必须将entt
分配给全局变量,然后在函数外部使用此变量。什么是func1
?代码对我来说是正确的。除了它找不到的func1
。return entt
未被注意到,因为按钮运行此函数,并且它没有获取此值的方法。您必须将entt
分配给全局变量,然后在函数外部使用此变量。非常感谢!!!这真的很有帮助,对于任何询问“func1”的人来说,这是在我压缩代码之前调用的另一个函数。非常感谢!!!这真的很有帮助,对于任何询问“func1”的人来说,这是在我压缩代码之前调用的另一个函数