gpt4 book ai didi

python - 确定当前在顶部的 tkinter 窗口

转载 作者:太空狗 更新时间:2023-10-30 02:49:00 25 4
gpt4 key购买 nike

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

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

我正在使用的一些代码:

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.checkbutton。目前,切换复选按钮可以打开或关闭窗口。但是,我真正想让它做的是,如果窗口在应用程序前面,则关闭窗口;如果窗口在应用程序后面,则将窗口调到前面。

希望这能解决一些问题。

提前致谢!

最佳答案

实际上可以检查窗口的堆叠顺序。使用 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')

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

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

返回 1,意思是“是的,窗口二在窗口根之上。”而以下内容:

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

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

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

它以类似这样的奇怪字符串的形式返回完整的窗口堆叠顺序:

'. .68400520L .68401032L'

当您运行命令时,这开始变得有意义:

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

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

关于python - 确定当前在顶部的 tkinter 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10343759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com