【问题标题】:Tkinter get names, attributes and values of object by reference numberTkinter 通过引用号获取对象的名称、属性和值
【发布时间】:2021-04-03 03:42:42
【问题描述】:

使用此答案中的代码:Get list of Toplevels on Tkinter:

'''
List all objects in play next song
https://stackoverflow.com/questions/60978666/get-list-of-toplevels-on-tkinter
'''
LAST_TIME=0.0
THING_COUNT=0

def toplevels(ventana):
    global LAST_TIME, THING_COUNT
    now = time.time()
    if not int(now) == int(LAST_TIME):
        if THING_COUNT > 0:
            print('Number of things:', THING_COUNT)
            THING_COUNT = 0
        print('\n============= toplevels() called at:', t(now),'=============')
        LAST_TIME = now
    for k, v in ventana.children.items():
        if isinstance(v, tk.Toplevel):
            print('Toplevel:', k, v)
        else:
            print('   other:', k, v)
        toplevels(v)
        THING_COUNT += 1

我这样称呼它:

    toplevels(root)

输出是这样的:

Number of things: 42

============= toplevels() called at: Dec 24 2020 12:03:27 =============
Toplevel: 140109521792176 .140109521792176
   other: 140109520829184 .140109521792176.140109520829184
   other: 140109520830264 .140109521792176.140109520829184.140109520830264
   other: 140109520859360 .140109521792176.140109520829184.140109520859360
   other: 140109520829472 .140109521792176.140109520829184.140109520829472
   other: 140109521432304 .140109521792176.140109521432304
   other: 140109520827600 .140109521792176.140109521432304.140109520827600
   other: 140109520828032 .140109521792176.140109521432304.140109520827600.140109520828032
   other: 140109520827888 .140109521792176.140109521432304.140109520827600.140109520827888
   other: 140109520828896 .140109521792176.140109521432304.140109520827600.140109520828896
   other: 140109520828176 .140109521792176.140109521432304.140109520827600.140109520828176
   other: 140109520828608 .140109521792176.140109521432304.140109520827600.140109520828608
   other: 140109520828392 .140109521792176.140109521432304.140109520827600.140109520828392
   other: 140109520767024 .140109521792176.140109521432304.140109520767024
   other: 140109520827384 .140109521792176.140109521432304.140109520767024.140109520827384
   other: 140109520767096 .140109521792176.140109521432304.140109520767024.140109520767096
   other: 140109520827096 .140109521792176.140109521432304.140109520767024.140109520827096
Toplevel: 140109520827168 .140109520827168
   other: 140109521621360 .140109520827168.140109521621360
   other: 140109521623016 .140109520827168.140109521621360.140109521623016
   other: 140109521621576 .140109520827168.140109521621360.140109521621576
   other: 140109521623160 .140109520827168.140109521621360.140109521623160
   other: 140109521623376 .140109520827168.140109521621360.140109521623376
   other: 140109521622152 .140109520827168.140109521621360.140109521622152
   other: 140109521622728 .140109520827168.140109521621360.140109521622728
   other: 140109521623232 .140109520827168.140109521621360.140109521623232
   other: 140109521621936 .140109520827168.140109521621360.140109521621936
   other: 140109521622800 .140109520827168.140109521621360.140109521622800
   other: 140109521622944 .140109520827168.140109521621360.140109521622944
   other: 140109521623448 .140109520827168.140109521621360.140109521623448
   other: 140109521623520 .140109520827168.140109521623520
   other: 140109521624888 .140109520827168.140109521623520.140109521624888
   other: 140109521624024 .140109520827168.140109521623520.140109521624024
   other: 140109521623664 .140109520827168.140109521623520.140109521623664
   other: 140109396840528 .140109520827168.140109521623520.140109396840528
   other: 140109521624456 .140109520827168.140109521623520.140109521624456
   other: 140109521624240 .140109520827168.140109521623520.140109521624240
   other: 140109521624672 .140109520827168.140109521623520.140109521624672
   other: 140109396840744 .140109520827168.140109521623520.140109396840744
   other: 140109396840960 .140109520827168.140109396840960
   other: 140109396841176 .140109520827168.140109396840960.140109396841176
   other: 140109396841392 .140109520827168.140109396840960.140109396841392

它正确地显示了两个顶层。首先是音乐库树形视图。 Second 当前正在播放四帧歌曲:

  • 专辑封面图片
  • 当前播放歌曲的详细信息(艺术家、专辑、曲目等)
  • 按钮(关闭、暂停/播放、随机播放、下一个、上一个等)
  • 年表树状视图(最近播放、当前播放、下一个播放)

如何转换机器语言参考:

other: 140109521624240 .140109520827168.140109521623520.140109521624240

转化为人类可读的格式,例如:

  • 这是一个 x 宽、y 高的窗口,安装在桌面的 x,y 处,name="self.play_top"
  • 这是 nsew 的框架,带有时髦的浮雕,name="playfrm"
  • 这是一个默认字体的标签,大小为 12 磅,包含“此文本”,文本变量="self.current_song_name"
  • 这是一个按钮,text="✘ Close", padx=2, pady=2, foreground="#0000000", background="#ffffff", internal name="blah blah"

我需要创建一个函数,以人类可读的格式呈现信息,并允许更改颜色、字体大小(hDPI 监视器)、主题等。然后将这些更改应用于另一个使用 .configure() 方法的函数。我还计划使用字典并存储在 pickle 配置文件中以供以后重新应用。

【问题讨论】:

  • 当您使用print(v) 时,您将实际对象转换为引用字符串。只需保存 v 对象即可。
  • @Novel 当你说“简单保存”时,这意味着将 print(v) 更改为 print(save(v)),但我认为你不是那个意思。
  • 好吧,我不知道你想对这些对象做什么,所以“保存”是指做任何你想做的事情。例如,如果你想打印出宽度,你可以print(v.winfo_width())
  • @Novel 这就像一个魅力!现在我必须弄清楚如何遍历对象具有 bseides winfo_width() 的所有属性。接下来我必须开始使用if 子句选择要打印的属性。
  • @Novel 感谢您的帮助,我发布了一个达到预期最终结果的答案。中间步骤(列出所有属性等)的答案会更长,因为诊断输出有数千行。

标签: python tkinter


【解决方案1】:

解决配置问题。在这个应用程序中,有一个带有 8 个没有名称的 tkinter 按钮的框架。每隔几分钟,图稿的背景颜色就会发生变化,并且需要向下传播到所有按钮。

这是执行此操作的函数:

def config_all_buttons(level, **kwargs):
    ''' Configure all tk buttons within a frame (doesn't work for toplevel?).

        level = frame name, eg self.play_btn

        **kwargs = tkinter_button.configure(keywords and values). For example:
            fg="#000000", bg="#ffffff", padx=5
    '''
    for k, v in level.children.items():

        if isinstance(v, tk.Button):
            if v["image"] == "":
                # We can't configure image labels that have a value
                v.configure(**kwargs)

        config_all_buttons(v, **kwargs)

这是从主线或类中调用函数的方式:

        self.play_frm_bg = self.play_resized_art.getpixel((3,3))
        hex_background = img.rgb_to_hex(self.play_frm_bg)
        self.play_frm_fg = img.contrasting_rgb_color(self.play_frm_bg)
        hex_foreground = img.rgb_to_hex(self.play_frm_fg)
        self.play_frm.configure(bg=hex_background)
        toolkit.config_all_labels(self.play_frm, fg=hex_foreground, \
                                  bg=hex_background)
        toolkit.config_all_buttons(self.play_btn, fg=hex_foreground, \
                                  bg=hex_background)

当艺术品具有“黑巧克力”色背景时,效​​果如下:

当艺术品具有“深橙色”颜色背景时,它的外观如下:

当艺术品具有“黄色”背景并强制文本为黑色时,效果如下:

【讨论】:

    猜你喜欢
    • 2013-01-02
    • 2014-09-19
    • 2014-09-23
    • 2012-11-17
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多