确定当前在顶部的窗口

2024-10-01 19:26:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python2.7和tkinter编写了一个应用程序。我创建了一个工具栏,其中有几个按钮可以打开显示各种选项的顶部窗口。我用过ttk.检查按钮以“toolbutton”样式作为指示器,显示选项窗口是打开还是关闭。在

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

我的一些代码:

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()

以下是我编写的整个应用程序的图片: enter image description here

每个窗口上都有各自的图像ttk.检查按钮. 此时,切换check按钮可以打开或关闭窗口。但是,我真正希望它做的是关闭窗口(如果窗口在应用程序前面),或者将窗口移到前面(如果它在应用程序后面)。在

希望这能澄清一些事情。在

提前谢谢!在


Tags: self应用程序applicationdef选项button按钮command
1条回答
网友
1楼 · 发布于 2024-10-01 19:26:45

实际上可以检查窗户的堆叠顺序。使用Tkinter,您必须执行一些有趣的tcl评估来获取信息。我在TkDoc找到了答案,在Windows and Dialogs部分,向下滚动,直到你得到“堆叠顺序”。这些代码一直困扰着我,直到我开始用它进行交互。我的测试代码是:

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

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

^{pr2}$

返回1,表示“是,窗口2位于窗口根之上。”而下面的值:

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

返回0,表示“不,窗口根不在窗口2之上。”您也可以使用命令:

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

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

'. .68400520L .68401032L'

当您运行以下命令时,这就开始有意义了:

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

然后计算出根的内部名称为“.”,一个是“.6840052L”,两个是“.68401032L”。您向后读取root.tk.eval('wm stackorder '+str(root))的输出,这样就意味着两个在上面,一个在下面,根在这两个下面。在

相关问题 更多 >

    热门问题