You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Tkinter如何获取多个Entry输入值供mainloop后代码使用

Tkinter主循环后获取输入值的方案

问题描述

(编辑:下方附最终可运行代码!)
需要实现带3个输入项的Tkinter输入窗口,提取用户输入的字符串,供root.mainloop()执行结束后的后续代码调用。已掌握Tkinter基础操作,但无法实现输入内容与主循环外代码的联动。
初始问题代码:

import tkinter as tk
from tkinter import ttk
   
def close_window():
    root.destroy()

def show_entry_fields():
    print("First %s\nSecond %s\nThird %s" % (e1.get(), e2.get(), e3.get()))
    firstinput = e1.get()
    secondinput = e2.get()
    thirdinput = e3.get()

root = tk.Tk()

tk.Label(root, text="First input").grid(row=0)
tk.Label(root, text="Second input").grid(row=1)
tk.Label(root, text="Third input").grid(row=2)

text1 = tk.StringVar()
text2 = tk.StringVar()
text3 = tk.StringVar()

e1 = tk.Entry(root, textvariable=text1)
e1.grid(row=0, column=1)

e2 = tk.Entry(root, textvariable=text2)
e2.grid(row=1, column=1)

e3 = tk.Entry(root, textvariable=text3)
e3.grid(row=2, column=1)

tk.Button(root, text='Enter', command=show_entry_fields).grid(row=3, 
                                                               column=1, 
                                                               sticky=tk.W, 
                                                               pady=4)

tk.Button(root, text='Close', command=close_window).grid(row=4,
                                                         column=1,
                                                         sticky=tk.W,
                                                         pady=4)

root.mainloop()

firstinput
print(firstinput)

运行代码后触发如下报错:

NameError                                 Traceback (most recent call last)
<ipython-input-1-76b7cc4daea7> in <module>
     42 root.mainloop()
     43 
---> 44 firstinput

NameError: name 'firstinput' is not defined

错误原因

  • 报错本质是变量作用域限制:show_entry_fields()函数内部定义的firstinput/secondinput/thirdinput属于局部变量,仅能在函数内部访问,主循环结束后的全局代码域无法读取这几个变量,因此触发名称不存在的错误。

解决方式

代码中已经在全局作用域定义了与输入框绑定的StringVar对象text1/text2/text3,用户输入的内容会实时同步到这些对象中,不需要在按钮回调函数内重复给局部变量赋值。用户完成输入关闭窗口、主循环结束后,直接调用这几个StringVar对象的.get()方法,就能拿到对应的输入字符串供后续逻辑使用。
最终可运行代码:

import tkinter as tk
from tkinter import ttk

def close_window():
    root.destroy()

def show_entry_fields():
    print("First %s\nSecond %s\nThird %s" % (e1.get(), e2.get(), e3.get()))

root = tk.Tk()

tk.Label(root, text="First input").grid(row=0)
tk.Label(root, text="Second input").grid(row=1)
tk.Label(root, text="Third input").grid(row=2)

text1 = tk.StringVar()
text2 = tk.StringVar()
text3 = tk.StringVar()

e1 = tk.Entry(root, textvariable=text1)
e1.grid(row=0, column=1)

e2 = tk.Entry(root, textvariable=text2)
e2.grid(row=1, column=1)

e3 = tk.Entry(root, textvariable=text3)
e3.grid(row=2, column=1)

tk.Button(root, text='Enter', command=show_entry_fields).grid(row=3, 
                                                               column=1, 
                                                               sticky=tk.W, 
                                                               pady=4)

tk.Button(root, text='Close', command=close_window).grid(row=4,
                                                         column=1,
                                                         sticky=tk.W,
                                                         pady=4)

root.mainloop()

firstinput = text1.get()
secondinput = text2.get()
thirdinput = text3.get()
print(firstinput)

优化提示:如果需要点击Enter按钮后自动关闭窗口,可以在show_entry_fields()函数末尾添加root.destroy(),无需额外点击Close按钮即可结束主循环,继续执行后续代码。

内容的提问来源于stack exchange,提问作者mmurer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 20:45:35