Reputation: 4383
I have created two widgets: a label and a canvas they are inside of a class and in the init method of the class. For some reason, none of the widgets show up and it just gives me a blank tkinter window.
def __init__ (self, master):
self.mousepos = 0,0
self.clickpos = 0,0
self.poslabel = Label(master, text = "Mouse Click: " \
+ str(self.clickpos) + "Cursor Point: "\
+ str(self.mousepos))
self.poslabel.pack()
self.fcanvas = Canvas(master,width=800,height=400, bg = 'grey90',\
borderwidth = 5, relief = RAISED)
self.fcanvas.bind("<Button-1>", self.pressButton1)
self.fcanvas.pack()
Upvotes: 0
Views: 4351
Reputation: 52941
For reference here is a working version of your code. Notice how I passed root for the master argument. Also, take note of how I used the .mainloop() method on root.
Tkinter runs in a loop, if you don't call the .mainloop() method your program won't run properly.
This should work:
from Tkinter import *
class MyWidgets :
def __init__ (self, master) :
self.mousepos = 0,0
self.clickpos = 0,0
self.poslabel = Label(master, text = "Mouse Click: " \
+ str(self.clickpos) + "Cursor Point: "\
+ str(self.mousepos))
self.poslabel.pack()
self.fcanvas = Canvas(master, width=800,height=400, bg = 'grey90',\
borderwidth = 5, relief = RAISED)
self.fcanvas.bind("<Button-1>", self.pressButton1)
self.fcanvas.pack()
def pressButton1 (self, event) :
print 'You pressed button 1!'
if __name__ == '__main__' :
root = Tk()
MyWidgets(root)
root.mainloop()
Upvotes: 2
Reputation: 385970
If that is all of your code, you're forgetting to run the mainloop
method of your root window. Window drawing happens via events that are processed by the event loop; if it doesn't run, the windows don't get the opportunity to draw themselves on the screen.
Upvotes: 2