获取 tkinter 中 Toplevel 窗口的数量

如何解决获取 tkinter 中 Toplevel 窗口的数量

有没有办法在 tkinter 中获取顶层窗口的数量

代码如下:

from tkinter import *

root = Tk()
root.geometry("500x500")

def get_number_of_toplevel_windows():
    # Code to count the number of toplevel windows
    pass

toplevel_1 = Toplevel(root)
toplevel_2 = Toplevel(root)
toplevel_3 = Toplevel(root)

get_button = Button(root,text = "Get number of toplevel windows",command = get_number_of_toplevel_windows)
get_button.pack()

mainloop()

在这里,当我点击 get_button 时,我想打印顶级窗口的数量(在本例中为三个。

有没有办法在 tkinter 中实现这一点?

如果有人能帮助我就好了。

解决方法

您可以只使用 winfo_children() 来调出所有子项,然后检查其中的 'toplevel',例如:

def get_number_of_toplevel_windows():
    tops = [] # Empty list for appending each toplevel
    for widget in root.winfo_children(): # Looping through widgets in main window
        if '!toplevel' in str(widget): # If toplevel exists in the item
            tops.append(widget) # Append it to the list
             
    print(len(tops)) # Get the number of items in the list,AKA total toplevels

这也不需要任何外部模块。

或者:

def get_number_of_toplevel_windows():
    tops = []
    for widget in root.winfo_children(): # Loop through each widget in main window
        if isinstance(widget,Toplevel): # If widget is an instance of toplevel
            tops.append(widget) # Append to a list
             
    print(len(tops)) # Get the number of items in the list,AKA number of toplevels

后一种方法似乎更有效,因为它检查项目的实例,并且不像第一种方法那样比较字符串。

,

此解决方案仅适用于 Windows 平台(需要 pywinauto 库):

import tkinter as tk
from pywinauto import Desktop

NAME = 'Program name'

def get_number_of_toplevel_windows():
    windows = Desktop(backend='uia').windows()
    print(len([w.window_text() for w in windows if w.window_text() == NAME])-1)

root = tk.Tk()
root.title(NAME)
tk.Toplevel(root)
tk.Toplevel(root)
tk.Toplevel(root)
tk.Button(root,text='Get number of toplevel windows',command=get_number_of_toplevel_windows).pack()
tk.mainloop()

输出:

3
,

这对我有用

from tkinter import *

root = Tk()
root.geometry("500x500")    
toplevel_1 = Toplevel(root)

count = 0 #Total count of Toplevels
def get_number_of_toplevel_windows(ventana):
    global count
    for a,b in ventana.children.items(): #Getting list of root window's children and using recursion 
        if isinstance(b,Toplevel):
            count+=1 #If toplevel then count += 1
        get_number_of_toplevel_windows(b)

def printresult():
    print(count)
get_button = Button(root,text = "Get number of toplevel windows",command = printresult)
get_button.pack()
get_number_of_toplevel_windows(root)
root.mainloop()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?