为什么不鼓励使用多个 Tk 实例? [英] Why are multiple instances of Tk discouraged?

查看:45
本文介绍了为什么不鼓励使用多个 Tk 实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的例子:

import tkinter as tk

root = tk.Tk()
root.title("root")

other_window = tk.Tk()
other_window.title("other_window")

root.mainloop()

并参见下面的示例,该示例背靠背而不是一次创建 Tk 的实例,因此 正好有一个 Tk 实例> 在任何给定时间:

and also see below example that creates instances of Tk back-to-back instead of at once, so there's exactly one instance of Tk at any given time:

import tkinter as tk

def create_window(window_to_be_closed=None):
    if window_to_be_closed:
        window_to_be_closed.destroy()
    window = tk.Tk()
    tk.Button(window, text="Quit", command=lambda arg=window : create_window(arg)).pack()
    window.mainloop()

create_window()

  • 为什么拥有多个 Tk 实例被认为是不好的?
  • 第二个片段是否被认为更好,或者它是否受到影响与第一个代码相同的条件?
    • Why is it considered bad to have multiple instances of Tk?
    • Is the second snippet considered a bit better, or does it suffer from the same conditions the first code does?
    • 推荐答案

      为什么拥有多个 Tk 实例被认为是不好的?

      Why is it considered bad to have multiple instances of Tk?

      Tkinter 只是一个 Python 包装器,它围绕着导入 Tk 库的嵌入式 Tcl 解释器.当您创建一个根窗口时,您就创建了一个 Tcl 解释器的实例.

      Tkinter is just a python wrapper around an embedded Tcl interpreter that imports the Tk library. When you create a root window, you create an instance of a Tcl interpreter.

      每个 Tcl 解释器都是一个独立的沙箱.一个沙箱中的对象不能与另一个沙箱中的对象交互.最常见的表现是在一个解释器中创建的 StringVar 在另一个解释器中不可见.小部件也是如此——您不能在一个解释器中创建小部件,而该解释器在另一个解释器中作为父小部件.图像是第三种情况:一个创建的图像不能用于另一个.

      Each Tcl interpreter is an isolated sandbox. An object in one sandbox cannot interact with objects in another. The most common manifestation of that is that a StringVar created in one interpreter is not visible in another. The same goes for widgets -- you can't create widgets in one interpreter that has as a parent widget in another interpreter. Images are a third case: images created in one cannot be used in another.

      从技术角度来看,您没有理由不能同时拥有两个 Tk 实例.反对它的建议是因为很少有实际需要有两个或更多不同的 Tcl 解释器,并且它会导致初学者难以掌握的问题.

      From a technical standpoint, there's no reason why you can't have two instances of Tk at the same time. The recommendation against it is because there's rarely an actual need to have two or more distinct Tcl interpreters, and it causes problems that are hard for beginners to grasp.

      第二个代码段是否更好一些,或者它是否与第一个代码相同?

      Is the second snippet considered a bit better, or does it suffer from the same conditions the first code does?

      如果不知道您要实现的目标,就不可能说问题中的第二个示例是否更好.这可能也好不到哪里去,因为再次重申,您实际上很少需要两个实例.

      It's impossible to say whether the second example in the question is better or not without knowing what you're trying to achieve. It probably is not any better since, again, there's rarely ever a time when you actually need two instances.

      在 99.9% 的情况下,最好的解决方案是只创建一个 Tk 实例,供您在程序的生命周期中使用.如果您需要第二个或后续窗口,请创建 Toplevel 的实例.很简单,这就是 tkinter 和底层 Tcl/Tk 解释器的设计用途.

      The best solution 99.9% of the time is to create exactly one instance of Tk that you use for the life of your program. If you need a second or subsequent window, create instances of Toplevel. Quite simply, that is how tkinter and the underlying Tcl/Tk interpreter was designed to be used.

      这篇关于为什么不鼓励使用多个 Tk 实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆