Tkinter 文件对话框打破了入口小部件 [英] Tkinter filedialog breaks entry widgets

查看:24
本文介绍了Tkinter 文件对话框打破了入口小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tl;dr:当应用程序调用 tkinter.filedialog 时,entry 字段没有正确聚焦.

详细解释:

初始化 tkinter 应用程序时,默认情况下启用 entry 字段.它们的状态是 tk.ENABLED,它们可以通过使用 tab 滚动字段来集中注意力,最重要的是,它们可以被点击以选择字段.>

出于某种原因,调用 tkinter.filedialog 会破坏此行为.如果调用了 tkinter.filedialog 的方法,例如 askdirectoryaskopenfile()entry 字段将仍然具有 tk.ENABLED 状态,背景将被正确设置样式,但单击输入字段不会插入光标或选择字段.当然,打字不会注册.

这可以通过切换到不同的窗口并切换回来来解决.然而,文件对话框窗口(正确地)将用户直接返回到主窗口,因此用户总是看到一个似乎被锁定的主窗口.

看这个例子:

将 tkinter 导入为 tk从 tkinter 导入文件对话框BR8K = 真根 = tk.Tk()如果 BR8K:filedialog.askdirectory()entry = tk.Entry(root,takefocus=True,highlightthickness=2)entry.grid(sticky="WE")root.mainloop()

此处,如果 BR8KFalse,则代码行为正常,如果 BR8KTrue,则代码行为不正确.

(注意:在生产环境中,这将是面向对象的.该问题在面向对象的环境中仍然存在.)

解决方案

这是在第一次到达 mainloop() 之前调用对话窗口导致的已知问题.

解决此问题的最简单方法是在对话框前添加 update_idletask().

试试这个:

导入 tkinter 作为 tk从 tkinter 导入文件对话框BR8K = 真根 = tk.Tk()# 通过添加它,您可以避免在 mainloop() 进行第一个循环之前调用对话框的焦点中断问题.root.update_idletasks()如果 BR8K:filedialog.askdirectory()entry = tk.Entry(root,takefocus=True,highlightthickness=2)entry.grid(sticky="WE")root.mainloop()

tl;dr: When the application calls tkinter.filedialog, entry fields do not properly focus.

Long explanation:

When initializing a tkinter application, the entry fields are enabled by default. Their state is tk.ENABLED, they can be focused on by scrolling through fields with tab, and, most importantly, they can be clicked on to select the field.

For some reason, this behavior is broken by calling tkinter.filedialog. If a method of tkinter.filedialog is called, such as askdirectory or askopenfile(), the entry field will still have the tk.ENABLED state, and the background will be properly styled, but clicking on the entry field will not insert the cursor or select the field. Typing, of course, does not register.

This can be worked around by toggling to a different window and toggling back. However, the file dialog windows (properly) return the user directly back to the main window, and so users are always presented with a main window that appears to be locked up.

See this example:

import tkinter as tk
from tkinter import filedialog

BR8K = True

root = tk.Tk()

if BR8K:
    filedialog.askdirectory()

entry = tk.Entry(root, takefocus=True, highlightthickness=2)
entry.grid(sticky="WE")


root.mainloop()

Here, the code behaves properly if BR8K is False, and incorrectly if BR8K is True.

(Note: In a production environment, this would be object oriented. The issue persists in an object oriented environment.)

解决方案

This is a known issues resulting from a dialog window being called prior to the mainloop() being reached for the first time.

The simplest way to fix this is to add update_idletask() before the dialog.

Try this:

import tkinter as tk
from tkinter import filedialog

BR8K = True

root = tk.Tk()
# By adding this you avoid the focus breaking issue of calling dialog before the mainloop() has had its first loop.
root.update_idletasks() 

if BR8K:
    filedialog.askdirectory()

entry = tk.Entry(root, takefocus=True, highlightthickness=2)
entry.grid(sticky="WE")


root.mainloop()

这篇关于Tkinter 文件对话框打破了入口小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆