What does calling Tk() actually do?

前端 未结 3 1763
天命终不由人
天命终不由人 2020-12-28 08:50

I was brushing up on Tkinter when I looked upon a minimal example from the NMT Tkinter 8.5 Reference.

#!/usr/bin/env python
import tkinter as tk

class Appli         


        
3条回答
  •  心在旅途
    2020-12-28 09:25

    Tkinter works by starting a tcl/tk interpreter under the covers, and then translating tkinter commands into tcl/tk commands. The main window and this interpreter are intrinsically linked, and both are required for a tkinter application to work.

    Creating an instance of Tk initializes this interpreter and creates the root window. If you don't explicitly initialize it, one will be implicitly created when you create your first widget.

    I don't think there are any pitfalls by not initializing it yourself, but as the zen of python states, "explicit is better than implicit". Your code will be slightly easier to understand if you explicitly create the instance of Tk. It will, for instance, prevent other people from asking the same question about your code that you just asked about this other code.

提交回复
热议问题