Two naive Tkinter questions

Andrew Koenig ark at acm.org
Sun Nov 2 21:10:55 CET 2003


from Tkinter import *

class Application(Frame):

    def setcolor(self):
        self["bg"] = "blue"

    def createWidgets(self):
        self.b1 = Button(self, bg = "red", command = self.setcolor)
        self.b1.place(height = 50, width = 50)

        self.b2 = Button(self, text = "Exit", command = self.quit)
        self.b2.place(height = 50, width = 50, x = 50)

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.place(height = 50, width = 100)
        self.createWidgets()

app = Application()
app.mainloop()


1) When I run this program, it displays two buttons.  When I click the
button on the left, I would like the color of that button to change from red
to blue.  This code is obviously the wrong way to accomplish this, because
when setcolor is called, it gets the button's parent, not the button itself.
How do I arrange for setcolor to get the right object?

2) The window in which these buttons appear is the wrong size, and does not
depend on the height and width given to self.place in __init__.  Yet the
height and width arguments do something, because if I set width to 75, it
cuts off half the right-hand button.  How do I say how large I want the
window to be?






More information about the Python-list mailing list