Tkinter 文件对话框正在窃取焦点,并且在没有“Alt-tab"的情况下不会返回焦点在 Python 3.4.1 中 [英] Tkinter filedialog is stealing focus and not returning it without "Alt-tab" in Python 3.4.1

查看:67
本文介绍了Tkinter 文件对话框正在窃取焦点,并且在没有“Alt-tab"的情况下不会返回焦点在 Python 3.4.1 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是第一个问题,但我已经回答了几个,所以希望这里有足够的信息供有人指导我正确的方向.

First question here, but I've answered a few, so hopefully there'll be enough information here for someone to give me a pointer in the right direction.

我有以下代码将构成我正在构建的应用程序的一部分.我希望用户能够输入他们的名字并评估一些图像,使用滑块对它们进行评分.为了定义图像的来源,我想使用 filedialog.askdirectory 选择一个文件夹,以便用户可以验证包含图像的正确文件夹.

I have the following code that will form part of an app I'm building. I want users to be able to enter their name and evaluate some images, scoring them using the slider. In order to define the source of the images, I want to select a folder using filedialog.askdirectory so the user can verify the correct folder containing the images.

我去掉了按钮和记录数据的功能以简化这一点.

I've stripped out the buttons and the functions that record data to simplify this.

所以,我遇到的问题是当窗口弹出时,文件对话框出现并允许我选择文件夹.但是,在我选择之后,无法选择 Entry 字段以允许用户输入他们的姓名.如果我通过 Alt-tab 离开窗口或单击另一个窗口并返回,我可以定位光标并照常进行 - 显然,这对我的最终用户来说并不理想.

So, the problem I have is that when the window pops up, the filedialog appears and allows me to choose the folder. After I've chosen though, the Entry fields are not able to be selected to allow the user to type in their name. If I navigate away from the window by Alt-tab or by clicking another window and going back, I can position the cursor and proceed as normal - obviously, this isn't ideal for my end-users.

这是此处显示的文件对话框弹出窗口:

Here is the filedialog popup shown here:

第二个窗口,它不会自动获得焦点:

And the second window, which doesn't automatically take focus:

import tkinter as tk
from tkinter import filedialog
import numpy as np

class Model:

    def __init__(self):

        self.scores = []
        self.position = 0
        self.first_name = ""
        self.last_name = ""


class WelcomeWindow:


    def __init__(self, master):

        button_width=25
        button_height=10
        self.master = master
        self.frame = tk.Frame(self.master)
        #self.file_dialog = tk.Frame(self.master)
        self.directory = filedialog.askdirectory(parent=self.master)
        self.canvas = tk.Canvas(self.frame,
                                height=600,
                                width=800
                               )
        self.model = Model()
        self.first_name_entry = tk.Entry(self.frame)
        self.last_name_entry = tk.Entry(self.frame)

        self.slider = tk.Scale(self.frame, 
                               length=button_width * 20,
                               width=button_width * 2,
                               sliderlength=150,
                               showvalue=False,
                               from_=-3.00,
                               to=3.00,
                               orient="horizontal",
                               tickinterval=1,
                               label="Here is a slider",
                               resolution=0.01
                              )
        self.frame.pack()
        self.first_name_entry.pack()
        self.last_name_entry.pack()
        self.canvas.pack()
        self.slider.pack()

def main():
    root = tk.Tk()
    win = WelcomeWindow(root)
    root.mainloop()

main()

我已经尝试将 framefirst_name_entry 字段的 takefocus 选项设置为 True没有成功.我还尝试将 self.directory 的父级调整为 self.frame,这完全消除了我与主窗口交互的能力.

I've tried setting the takefocus option for the frame and for the first_name_entry field to True with no success. I've also tried adjusting the parent of self.directory to self.frame and that completely removes my ability to interact with the main window.

如果我遗漏了任何内容,请发表评论,我会尽我所能

If I've left anything out, please leave a comment and I'll provide anything I can

推荐答案

在 Windows 系统上存在一个已知问题,即在主循环第一次有机会完全循环之前使用 filedialog 会导致此类焦点问题.

There is a known issues on Windows systems where using filedialog before the mainloop has had a chance to fully loop the first time causing this kind of focus issue.

>

最初我使用 after() 解决了这种问题,以安排文件对话框在第一个循环完成后的某个时间发生,但感谢 fhdrsdg 的 评论有一种更简单的方法可以使用 update_idletasks() 解决此问题.

Originally I solved this kind of issue using after() to schedule the filedialog to happen sometime after the first loop has completed but thanks to fhdrsdg's comment there is a simpler method to fix this using update_idletasks().

这是您重新编写的代码以修复焦点问题并进行一些常规清理.

Here is your code reworked to fix the focus issue and some general clean up.

import tkinter as tk
from tkinter import filedialog


class Model:
    def __init__(self):
        self.scores = []
        self.position = 0
        self.first_name = ""
        self.last_name = ""


class WelcomeWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        button_width = 25
        button_height = 10
        self.frame = tk.Frame(self)
        self.directory = ''
        self.canvas = tk.Canvas(self.frame, height=600, width=800)
        self.model = Model()
        self.first_name_entry = tk.Entry(self.frame)
        self.last_name_entry = tk.Entry(self.frame)
        self.slider = tk.Scale(self.frame, length=button_width * 20, width=button_width * 2, sliderlength=150, showvalue=False,
                               from_=-3.00, to=3.00, orient="horizontal", tickinterval=1, label="Here is a slider", resolution=0.01)
        self.frame.pack()
        self.first_name_entry.pack()
        self.last_name_entry.pack()
        self.canvas.pack()
        self.slider.pack()
        self.update_idletasks() # adding this here fixes the focus issue
        self.directory = filedialog.askdirectory()


def main():
    WelcomeWindow().mainloop()

if __name__ == "__main__":
    main()

这篇关于Tkinter 文件对话框正在窃取焦点,并且在没有“Alt-tab"的情况下不会返回焦点在 Python 3.4.1 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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