簡體   English   中英

Tkinter 文本框在 window 首次打開時不可點擊

[英]Tkinter textbox not clickable when window first opened

因此,當我第一次打開 window 時,其他一切都很好,我可以單擊按鈕,但當我嘗試單擊文本框並在其中輸入內容時,cursor 沒有出現,我無法輸入任何內容。 但是,如果我切換到任何其他窗口(實際上是從 chrome 到記事本的任何內容)並切換回來,我就可以在文本框中正常輸入。

我不知道如何解決這個問題。 有人可以幫我嗎? 我的 Python 版本是 3.8.0。

這是我的代碼的一部分,“頂部”是我的 tkinter 窗口的變量名。 inputtxt1 和 inputtxt2 是我遇到問題的文本框。

load_txt.set("analyze")
a_bt = Button(top, text = "analyze", command = _analyze_)
a_bt.config(textvariable=load_txt)
a_bt.grid(row=4, column=0, sticky='E')
dir_bt = Button(top, text = "change dir", command = ch_dir)
dir_bt.grid(row=4, column=1, sticky='W')
save_button['command'] = save_change
save_button.grid(row=5, column=0, sticky='E')
top.geometry("600x1000")
top.resizable(width = True, height = True)

Label(top, text = "start_slab").grid(row=0, column=0, sticky='E')     
inputtxt1 = Text(top, height = 1, width = 5, state='normal')
inputtxt1.grid(row=1, column=0, sticky='E')

Label(top, text = "end_slab").grid(row=2, column=0, sticky='E')
inputtxt2 = Text(top, height = 1, width = 5, state='normal')
inputtxt2.grid(row=3, column=0, sticky='E')

avg_bt = Button(top, text = "avg", command = show_avg)
prev_bt = Button(top, text = "prev", command = show_prev)
next_bt = Button(top, text = "next", command = show_next)
#panel.pack()

dir_name = askdirectory()

top.bind("<Return>", analyze)

top.grid_rowconfigure(0, weight=0)

top.mainloop()

謝謝!

===========編輯===========

這是代碼的簡短工作版本

from tkinter.filedialog import askdirectory
from tkinter import Tk, Label, Button, Text

frame_idx = 0
dir_name = None

top = Tk()
save_button = Button(top, text = "save images")

a_bt = Button(top, text = "analyze")
a_bt.grid(row=4, column=0, sticky='E')
dir_bt = Button(top, text = "change dir")
dir_bt.grid(row=4, column=1, sticky='W')
save_button.grid(row=5, column=0, sticky='E')
top.geometry("600x1000")
top.resizable(width = True, height = True)

Label(top, text = "start_slab").grid(row=0, column=0, sticky='E')     
inputtxt1 = Text(top, height = 1, width = 5, state='normal')
inputtxt1.grid(row=1, column=0, sticky='E')

Label(top, text = "end_slab").grid(row=2, column=0, sticky='E')
inputtxt2 = Text(top, height = 1, width = 5, state='normal')
inputtxt2.grid(row=3, column=0, sticky='E')

dir_name = askdirectory()


top.grid_rowconfigure(0, weight=0)

top.mainloop()

似乎當我沒有 askdirectory function 時,這不會成為問題,但如果我這樣做,就會出現問題。

在 dir_name dir_name = askdirectory()之前添加top.update()top.update_idletasks() () 。 我認為askdirectory會干擾條目,因此它們會停止處理事件,直到它們在屏幕上重繪(當您重新聚焦窗口時)。 那可能是一個tcl錯誤? 我不知道在調用askdirectory時 tcl 如何處理事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM