[英]uneditable entry box(widget) in tkinter after using in if-condition
[英]Tkinter Entry widget in Python is uneditable
當我運行此代碼時,文件選擇器出現,然后當我完成它時,我無法輸入條目小部件,直到我專注於另一個窗口然后返回。 為什么會這樣?
import tkinter as tk
from tkinter.filedialog import askopenfilename
location = ''
start = tk.Tk()
tk.Label(text='What is the name of your table?').pack()
box = tk.Entry(start, exportselection=0, state=tk.DISABLED)
box.pack()
button = tk.Button(start, text='OK', command=lambda e: None)
button.pack()
location = askopenfilename(defaultextension='.db',
title="Choose your database",
filetypes=[('Database Files', '.db'), ('All files', '*')])
box.config(state=tk.NORMAL)
start.mainloop()
你只box.focus_force()
在box.pack()
box.focus_force()
下面寫下box.focus_force()
,這應該可以幫到你。
這應該解決它
import tkinter as tk
from tkinter.filedialog import askopenfilename
location = ''
root = tk.Tk()
root.withdraw()
location = askopenfilename(defaultextension='.db', title="Choose your database", filetypes=[('Database Files', '.db'), ('All files', '*')])
start = tk.Tk()
tk.Label(start, text='What is the name of your table?').pack()
box = tk.Entry(start, exportselection=0, state=tk.DISABLED)
box.pack()
start.focus_set()
box.focus_set()
start.focus_force()
button = tk.Button(start, text='OK', command=lambda e: None)
button.pack()
box.config(state=tk.NORMAL)
start.mainloop()
首先運行askopenfilename
可以避免此問題。
在這樣做的過程中,您需要創建一個root
窗口並將其取出,否則您將獲得兩個窗口。
通過使用focus_set
和focus_force
您可以立即使用該框。
您需要執行以下 2 個步驟:
box.pack()
下面寫box.focus_force()
box.pack()
。location = askopenfilename(blah blah..)
並粘貼到start = tk.Tk()
你會很高興的!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.