Hi I have a little question about Label in tkinter.
When you use Label outside classes, you do something like
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text = "something", background = "something")
label.pack()
However, when it's inside a class and the code goes something like
import tkinter as tk
class Example(tk.Frame):
COLOURS = [ "#f45", "#ee5", "#aa4", "#a1e433", "#e34412", "#116611",
"#111 eeefff", "#3aa922191", "#abbabbaaa" ]
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
col = 1
for colour in Example.COLOURS:
#
label = tk.Label(self, text=colour, background=colour)
#
label.grid(row=1, column=col)
col += 1
def main():
root = tk.Tk()
ex = Example(root)
root.geometry("+300+300")
root.mainloop()
if __name__ == '__main__':
main()
but shouldn't it be rather like
label = tk.Label(self.parent, text=colour, background=colour)
since self.parent would correspond to root? When I try to do that, I get an error and I only do when I have the label.grid(...) line under it(I tried pack and it worked fine).
So I thought this code
import tkinter as tk
root = tk.Tk()
label = tk.Label(root)
label.grid(row=0, column=0)
root.mainloop()
wouldn't work either, but it actually worked fine. So I'm confused. Can anyone explain?