确定当前在顶部的 tkinter 窗口 [英] Determining what tkinter window is currently on top

查看:23
本文介绍了确定当前在顶部的 tkinter 窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 python 2.7 和 tkinter 编写了一个应用程序.我创建了一个带有多个按钮的工具栏,这些按钮可以打开显示各种选项的相应顶部窗口.我使用带有工具按钮"样式的 ttk.Checkbutton 作为指示器来显示选项窗口是打开还是关闭.

I have written an application in python 2.7 and tkinter. I created a tool bar with several buttons that open up respective top windows that display various options. I used ttk.Checkbutton with the 'toolbutton' style as an indicator to show whether the option windows are open or closed.

问题是如果选择了另一个窗口,则选项窗口将移到后面.目前,如果再次选择工具按钮,选项窗口将关闭.但是,如果它在顶部,我只想关闭窗口.如果选项窗口不在顶部,我希望窗口移动到前面.

The problem is that the option windows will go to the back if another window is selected. Currently, if one selects the toolbutton again, the option window will close. However, I only want to close the window if it is on top. If the option window is not on top, I want the window to moved to the front.

我使用的一些代码:

class MainWindow:
    def __init__(self,application):
        self.mainframe=tk.Frame(application)
        application.geometry("900x600+30+30")

        self.otherOptionsSelect=tk.IntVar()
        self.otherOptions_Button=ttk.Checkbutton(application,style='Toolbutton',variable=self.otherOptionsSelect,
                                                onvalue=1, offvalue=0,image=self.optionsIcon, command=self.otherOptions)
    def otherOptions(self):

        if self.otherOptionsSelect.get()==0:
            self.otherOptions.destroy()
            return

        self.otherOptions=tk.Toplevel()
        self.otherOptions.title("IsoSurface Options")
        self.otherOptions.geometry("200x165+"+str(int(application.winfo_x())+555)+"+"+str(int(application.winfo_y())+230))

        self.otherOptApply_button=ttk.Button(self.otherOptions,text="Apply",command=self.showFrame)
        self.otherOptApply_button.place(x=20,y=80,width=50,height=30)

        self.otherOptClose_button=ttk.Button(self.otherOptions,text="Close",command=self.otherOptionsClose)
        self.otherOptClose_button.place(x=80,y=80,width=50,height=30)

    def otherOptionsClose(self):
        self.otherOptionsSelect.set(0)
        self.otherOptions.destroy()

这是我编写的整个应用程序的图片:

Here is a picture of the entire application I have written:

在上图中,每个窗口都有各自的 ttk.checkbutton.目前,切换复选按钮可以打开或关闭窗口.但是,我真正想让它做的是,如果窗口在应用程序前面,则关闭窗口,如果窗口在应用程序后面,则将窗口移到前面.

In the above image, each window has their respective ttk.checkbutton. At the moment, toggling the checkbutton either opens or closes the window. However, what I really want it to do is close the window if the window is in front of the application, or bring the window to the front if it is behind the application.

希望这能解决一些问题.

Hopefully this clears some things up.

提前致谢!

推荐答案

实际上可以检查窗口的堆叠顺序.使用 Tkinter,您必须执行一些有趣的 tcl 评估才能获取信息.我在 TkDoc 的 Windows 和对话框部分找到了答案,向下滚动直到你得到到堆叠顺序".代码让我感到困惑,直到我开始以交互方式使用它.我的测试代码是:

It is in fact possible to check stacking order of windows. Using Tkinter, you have to do some funny tcl evals to get at the information. I found the answer at TkDoc in the section on Windows and Dialogs, scroll down until you get to "Stacking Order". The code baffled me until I started playing around with it interactively. My test code was:

import Tkinter as tk
root = tk.Tk()
root.title('root')
one = tk.Toplevel(root)
one.title('one')
two = tk.Toplevel(root)
two.title('two')

然后我操纵窗户,使两个在上面,一个在下面,根在它们下面.在该配置中,以下奇怪之处可以告诉您窗口的相对分层:

I then manipulated the windows so that two was on top, one under that and root below them all. In that configuration, the following weirdness can tell you relative layering of windows:

root.tk.eval('wm stackorder '+str(two)+' isabove '+str(root))

返回 1,意思是是的,窗口 2 位于窗口根目录之上."而以下:

returns 1, meaning "Yes, window two is above window root." While the following:

root.tk.eval('wm stackorder '+str(root)+' isabove '+str(two))

返回 0,意思是不,窗口根不在窗口二之上."您也可以使用以下命令:

returns 0, meaning "No, window root is not above window two." You can also use the command:

root.tk.eval('wm stackorder '+str(root))

它以奇怪的字符串形式返回完整的窗口堆叠顺序,如下所示:

Which gives back the full window stacking order in the form of a weird string something like this:

'. .68400520L .68401032L'

当你运行命令时开始有意义:

Which starts to make sense when you run the commands:

str(root)
str(one)
str(two)

并找出 root 的内部名称是.",一个是.68400520L",两个是.68401032L".您向后读取 root.tk.eval('wm stackorder '+str(root)) 的输出,所以它说两个在上面,一个在下面,root 在两个下面.

and figure out that root has the internal name '.', one is '.68400520L' and two is '.68401032L'. You read the output of root.tk.eval('wm stackorder '+str(root)) backwards so it's saying two is on top, one is under that and root is below both.

这篇关于确定当前在顶部的 tkinter 窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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