python - 如何在 tkinter 窗口中打印项目?
问题描述
我有这个代码:
root= tk.Tk()
button1 = tk.Button(root, text='Button', command=lambda:print("Click"))
button1.grid(row=1, column=1)
Label = tk.Label(root, text="Text")
Label.grid(row=1, column=2)
root.mainloop()
我想得到什么:
Button:(1, 1), Label:(1, 2)
如何在根目录中打印项目及其目的地?
解决方案
如果您只想获取有关的信息grid
,使用grid_info()
可以获取有关小部件的信息。要获取所有小部件root
,只需使用root.winfo_children()
。
import tkinter as tk
root = tk.Tk()
button1 = tk.Button(root, text='Button', command=lambda:print("Click"))
button1.grid(row=1, column=1)
Label = tk.Label(root, text="Text")
Label.grid(row=1, column=2)
for widget in root.winfo_children():
print(f"{widget.widgetName}:({ widget.grid_info()['row']}, {widget.grid_info()['column']})")
root.mainloop()
结果:
button:(1, 1)
label:(1, 2)
推荐阅读
- regex - 使用 UAM(用户访问管理器)插件在 Wordpress 站点中访问带有大写字母的文件时出错
- java - 如何将准备好的语句从电子表格行项目循环到 derby 数据库
- php - 我无法插入新值
- visual-studio-code - VSCode - Ctrl + Alt + L 添加奇怪的字符。需要摆脱它
- python - 使用 While 循环列出超出范围的索引
- java - 如何在多个对象上设置 S3 存储桶策略?
- java - ScheduledThreadPoolExecutor 线程在完成后保留
- verilog - 在verilog代码中可以是几个supply1网吗?如果是,是什么意思?
- selenium - IWebDriver 在单击按钮后打开一个空白数据窗口,而不是加载的内容,假设是 - selenium c#
- java - 如何在春季远程处理中设置动态标题