在 Python Tkinter 中保留输入字段 [英] Retain entry field in Python Tkinter
问题描述
我的 GUI 中有一个弹出文本输入字段,要求输入两个条目 #1 文件路径 #2 项目(这些引用了 Excel 表)
I have a pop up text entry field as part of my GUI asking for two entries #1 Filepath #2 Item (these reference an excel sheet)
文件路径很少更改,但仍需要在需要时更改它的能力
The filepath will rarely change but the ability to change it when needed is still required
是否可以在输入字段中保留文件路径,这样就不需要每次都输入?即关闭并重新打开输入字段后仍包含 C:\*\*\*\*\*\somthing.xlsx
.
Is it possible to retain the filepath in the entry field so it dosnt require typing each and every time? i.e after closing and re opening the entry field still contains C:\*\*\*\*\*\somthing.xlsx
.
master = Tk()
master.title("Entry Field")
Label(master, text="Filepath: ").grid(row=0)
Label(master, text="Item Number: ").grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
def entry_field():
print("Filepath: %s" % (e1.get()))
print("Item No: %s" % (e2.get()))
Button(master,
text = 'Accept',
command = entry_field).grid(row=3, column=1, sticky=W, pady=4)
root.mainloop()
推荐答案
tk.Entry 小部件将保留这些值,直到您将它们重置为新值(您可以在按 Accept
时看到这一点)按钮多次而不修改 Entry
;因此您不需要做任何特殊的事情.
The tk.Entry widget will keep the values until you reset them to a new value (you can see this when pressing the Accept
button several times without modifying the Entry
; you therefore do not need to do anything special.
您可以主动在条目中插入默认值:
You could pro-actively insert a default value in your entry:
import tkinter as tk
def entry_field():
print("Filepath: %s" % (e1.get()))
print("Item No: %s" % (e2.get()))
master = tk.Tk()
master.title("Entry Field")
tk.Label(master, text="Filepath: ").grid(row=0)
tk.Label(master, text="Item Number: ").grid(row=1)
e1 = tk.Entry(master)
e2 = tk.Entry(master)
e1.insert(0, 'C:*****\somthing.xlsx') # <-- this string will be inserted in the Entry by default
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
tk.Button(master, text = 'Accept', command = entry_field).grid(row=3, column=1,
sticky=tk.W, pady=4)
master.mainloop()
这是该条目现在默认显示的方式:
This is how the entry appears by default now:
这篇关于在 Python Tkinter 中保留输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!