Reputation: 139
#game class
import Tkinter as tk
class Game(tk.Canvas):
def __init__(self, master):
canvas = tk.Canvas(master)
canvas.pack()
button = tk.Button(canvas, text='Quit', command=self.quit_game)
button.pack()
def quit_game(self):
root.destroy()#Should i put something else here?
root = tk.Tk()
game = Game(root)
root.mainloop()
Is it good practice, or, in other words, is there a problem with inheriting from canvas directly instead of frame, if for example I am not going to add any widgets except the canvas?
Another question I have is regarding the root.destroy()
. I don't understand why I can't say master.destroy()
or something to that effect.
Upvotes: 1
Views: 603
Reputation: 386210
There is nothing wrong with inheriting from Canvas
or any other Tkinter widget.
re master.destroy()
vs root.destroy()
: you can call it however you want. You simply need a reference to the root window. If you call it root
, to destroy it you would call root.destroy()
.
In general you should avoid the use of global variables. Given that you're passing in the root widget to your class, you can save a reference and use that instead:
class Game(tk.Canvas):
def __init__(self, master):
self.master = master
...
def quit_game(self):
self.master.destroy()
Upvotes: 1