which doesn't make sense, the first example (which I use) or the second ?
I think both of them. I am not clear what Myclass represents. Usually (but not always) a class object represents something concret, for instance a window.
the tutorials I studies were like that -- typically root or app = Tk()
I agree that many sample codes use this sort of structure. It is a style that I personally dislike but does work and many people use it. For instance this very simple program does work.
Code: Select all
import tkinter as tk
root = tk.Tk()
tk.Label(root,text = 'Hello world').pack()
from tkinter import *
root = Tk()
The import statement has a number of forms. This one imports all the definitions in tkinter even iff you only use a few. In more complicated programs there is a danger that tkinter will use a name that you also use. This can lead to errors.
imports tkinter but when you use an identifier from tkinter you must qualify it with tk. (e.g.tk.Tk()). This avoids the name clash problem. Again it is a matter of personal style.
In one of your codes you set
and in another
In the second you are passing root to the constructor(__init__) as a parameter. It is not clear to me what you do with that parameter. In any case root is a global variable so you can use it anyway.
everything works fine the way I have it (never an error) -- just want to clarify some things.
Good - in my view it is good for a program to work but even better if you understand why it works.