简体   繁体   English

无法在 tkinter 条目小部件(python)中输入文本

[英]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:我已经让它工作了,但是有错误:

  1. The text entry field comes pre-populated and when clicked it clears (it's meant to do this) but the cursor doesn't appear and I can't enter new text in the entry.文本输入字段已预先填充,单击时会清除(它的意思是这样做),但 cursor 没有出现,我无法在条目中输入新文本。 If I click off the tkinter window and then back onto it the cursor appears and I can enter text as intended如果我单击 tkinter window 然后返回 cursor 出现,我可以按预期输入文本
  2. The top radio button appears pre-selected, except it isn't.顶部的单选按钮显示为预先选择的,但不是。 The validate function says no radio button is selected.验证 function 表示未选择单选按钮。 I can select another option and it works, but I can't select the first option, even if I click off the first option and then back on to it.我可以 select 另一个选项并且它可以工作,但我不能 select 第一个选项,即使我单击第一个选项然后返回它。 EDIT: This problem was caused by my validate function, which discarded 0 values ie the top radio button.编辑:这个问题是由我的验证 function 引起的,它丢弃了 0 值,即顶部单选按钮。 Thanks @acw1668 for the clue to solve this one.感谢@acw1668 提供解决此问题的线索。 I've now set the starting value to -1 (so nothing is selected) and the validate function checks for -1我现在将起始值设置为 -1(因此没有选择任何内容)并且验证 function 检查 -1

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM