[英]Can't type text into tkinter entry widget (python)
I'm using tkinter to get a file path to open and then ask the user two questions, name via a text entry widget and course via some radiobuttons.我正在使用 tkinter 来获取要打开的文件路径,然后问用户两个问题,通过文本输入小部件命名,并通过一些单选按钮进行课程。 The code for the radio buttons I got from the answer to this thread and it worked great: How do I display a dialog that asks the user multi-choice question using tkInter?我从该线程的答案中获得的单选按钮的代码效果很好: 如何使用 tkInter 显示一个询问用户多项选择问题的对话框? Then I decided to add the Entry widget to get the answer to the name question and have been struggling.然后我决定添加 Entry 小部件以获得名称问题的答案并且一直在苦苦挣扎。 I've got it working, but with bugs:我已经让它工作了,但是有错误:
I can't figure out where to go from here.我不知道从这里到 go 的位置。 I've tried using e1.focus()
but it made no difference.我试过使用e1.focus()
但没有任何区别。 I've tried running the questions window before the fileopen dialog but that really confused it.我已经尝试在打开文件对话框之前运行问题 window 但这真的很困惑。 I've tried commenting out the clear entry function in case that was causing the problem but the behaviour was basically the same.我已经尝试注释掉清除条目 function 以防万一导致问题但行为基本相同。 EDIT: I'm using Spyder from Anaconda 3 as my IDE, I'm wondering if this is causing problem 1 as @acw1668 couldn't replicate it.编辑:我正在使用来自 Anaconda 3 的 Spyder 作为我的 IDE,我想知道这是否会导致问题 1,因为 @acw1668 无法复制它。
Here is my code:这是我的代码:
root=tk.Tk()
csvfp=tk.filedialog.askopenfilename()
SName, ProgSel=ask_multiple_choice_question(root, LPrograms)
def ask_multiple_choice_question(root, options):
#https://stackoverflow.com/questions/42581016/how-do-i-display-a-dialog-that-asks-the-user-multi-choice-question-using-tkinter
#Define a function to clear the contents of the textbox when it is clicked
def click(event):
e1.configure(state=tk.NORMAL)
e1.delete(0,tk.END)
e1.unbind('<Button-1>',clicked)
#checks entries are valid when submit button is clicked, then closes window
def validate():
if v.get() == 0 or v2.get() == "" or v2.get()==starttext:
return None
root.destroy()
frame1=Frame(root)
frame1.pack(padx=20,pady=10)
frame2=Frame(root)
frame2.pack()
frame3=Frame(root)
frame3.pack(pady=10)
Label(frame1, text="Student name?").pack()
v2=tk.StringVar()
e1 = Entry(frame1, width=30, textvariable=v2)
starttext="Surname, Firstname (ID)"
e1.insert(0, starttext)
e1.pack()
#bind e1 with mouse button to clear contents on click
clicked=e1.bind('<Button-1>',click)
Label(frame2, text="Program?").pack()
v = IntVar()
for i, option in enumerate(options):
Radiobutton(frame2, text=option, variable=v, value=i).pack(anchor="w")
Button(frame3, text="Submit", command=validate).pack()
root.mainloop()
return v2.get(), options[v.get()]
Thanks for any clues!感谢您提供任何线索!
For issue #1, you need to show the file dialog after the main window is ready and visible.对于问题 #1,您需要在主 window 准备好并可见后显示文件对话框。 You can call root.wait_visibility()
after root = tk.Tk()
:您可以在root = tk.Tk()
root.wait_visibility()
() :
...
root = tk.Tk()
root.wait_visibility()
...
For issue #2, since the value
option of the first radio button is 0 and the initial value of its tkinter variable v
is default 0, so the first radio button will be selected.对于问题 #2,由于第一个单选按钮的value
选项为 0,并且其 tkinter 变量v
的初始值默认为 0,因此将选择第一个单选按钮。 Set the initial value of v
to -1 will fix it:将v
的初始值设置为 -1 将修复它:
...
def validate():
# check -1 instead of 0
if v.get() == -1 or v2.get() == "" or v2.get() == starttext:
return None
root.destroy()
...
v = tk.IntVar(value=-1)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.