如何自动激活Tkinter SimpleDialog弹出窗口? [英] How to auto-activate a tkinter simpledialog pop-up window?

查看:539
本文介绍了如何自动激活Tkinter SimpleDialog弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个python脚本中包含此功能,该脚本抛出Tkinter简单对话框屏幕,以请求一些简单的用户输入.该功能有效.但是,它有 2个问题.

I have this function inside one of my python scripts which throws up a Tkinter simple dialog screen to ask for some simple user-input. The function works. However, there are 2 problems with it.

  1. 它打开了两个窗口,而我只需要一个窗口.但是,如果我删除 master = Tk() ,则会收到错误消息:

AttributeError:"NoneType"对象没有属性"winfo_viewable"

AttributeError: 'NoneType' object has no attribute 'winfo_viewable'

最好在某一点指出一个问题,但我的主要问题是第二个问题:

It would be nice to at one point figure that one out, but my main problem however is the second one:

  1. 无论何时打开简单对话框屏幕,我都必须先单击它,然后才能激活它,这很烦人.要解决此问题,我尝试在此处
  1. Whenever the simple dialog screen turns up, I have to click it first before it gets activated, which is annoying. To fix it I tried the solutions offered here and here but they do not work. The first link didn't do anything for me at all, the second link helped me to lift the master.Tk() window to the front, but that is not what I need. I need the simple dialog window to become the topmost window and I need it to be auto-activated so that when I run my code and the screen pops-up I can automatically type in it without having to click on it first.

任何帮助将不胜感激!

Any help would be greatly appreciated!

我的代码:

def getUser():
    master = Tk()
    newList2=str(newList).replace(", ","\n")
    for ch in ['[',']',"'"]:
        if ch in newList2:
            newList5=newList2.replace(ch,"")
    userNr=simpledialog.askinteger("Enter user number", newList2)
    chosenUsernr= userNr - 1
    global chosenUsernrdef
    chosenUsernrdef = chosenUsernr
    master.destroy()

推荐答案

首先,感谢Lafexlos显示了如何通过重新创建这样的窗口来在Tkinter simpledialog.askinteger()窗口上应用.lift()和类似命令的解决方案.作为Tk()实例.

First, credits to Lafexlos for showing a solution of how to apply .lift() and similar commands on a Tkinter simpledialog.askinteger() window by recreating such a window as a Tk() instance.

对于那些想如何自动激活Tk窗口的用户(这样您就不必在键入之前就可以单击它了),似乎有多个选择.

For those however looking how to automatically activate a Tk-window (so you do not have to click on it before being able to type in it), there appear to be multiple options.

最常见的解决方案似乎是使用.focus().force_focus(),已实现 此处.但是,在此处看来,这些选项可能不起作用(至少某些版本Windows OS. 问题显示了这些系统的可能解决方案.此外,以前的解决方案似乎无法在OS X的多个版本上使用.基于提供的解决方案

The most common solution seems to be to use .focus() or .force_focus() as seen implemented here and here. However over here it seems those options may not work on (at least some versions of) Windows OS. This question shows a possible solution for those systems. Also, the previous solutions appear not to work on multiple versions of OS X. Based on the solution offered here, using Apple's osascript, I was able to solve my problem.

工作代码最终看起来像这样:

The working code eventually looks like this:

def getUser():
    master = Tk()
    newList2=str(newList).replace(", ","\n")
    for ch in ['[',']',"'"]:
        if ch in newList2:
            newList2=newList2.replace(ch,"")
    cmd = """osascript -e 'tell app "Finder" to set frontmost of process "Python" to true'"""
    def stupidtrick():
        os.system(cmd)
        master.withdraw()
        userNr=simpledialog.askinteger("Enter user number", newList2)
        global chosenUsernrdef
        chosenUsernr= userNr - 1
        chosenUsernrdef = chosenUsernr
    stupidtrick()
    master.destroy()

简化/常规解决方案:

import os
from tkinter import Tk
from tkinter import simpledialog

def get_user():
    root = Tk()
    cmd = """osascript -e 'tell app "Finder" to set frontmost of process "Python" to true'"""
    def stupid_trick():
        os.system(cmd)
        root.withdraw()
        new_window=simpledialog.askinteger("Title of window", "Text to show above entry field")
    stupid_trick()
    root.destroy()

get_user()

编辑:现在,我正在寻找寻找解决方案的内容,似乎已经有多个帖子找到了.对于OS X上希望同时运行多个Tkinter和/或python实例时激活特定Tkinter窗口的用户,您可能需要查看这里.

EDIT: Now I am figuring out what to look for the solution appears to be found already by multiple posts. For those on OS X wanting to activate a specific Tkinter window when multiple instances of Tkinter and/or python are running simultaneously, you might want to look here.

这篇关于如何自动激活Tkinter SimpleDialog弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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