Trouble with Tk buttons

Matthew Dixon Cowles matt at sake.mondoinfo.com
Tue Aug 21 22:55:45 EDT 2001


On Tue, 21 Aug 2001 10:24:24 -0700 (PDT), David AraSmith
<mycerius at yahoo.com> wrote:

>I have a small program that I am writing to help me
>learn Tk. Basicly, I have a frame (inside another
>frame)with grid layout that has 2 buttons on the first
>row. When you click on one button, another row of
>buttons appear (in row 2). The same thing happens when
>you click the second button but they replace the first
>second row and vice versa. My problem is that when the
>second button is pressed and the "new" second row is
>displayed, the original second row still shows if the
>text is longer.
>Here is the code:

[. . .]

Dave,

I think that your problem is that when you create the new buttons, you
don't get rid of the old ones first. You'll want to send the old ones
a grid_forget() before you add the new ones. In order to send the
grid_forget(), you'll need to keep a reference to them around
somewhere in your object.

>I have also tried just create 2 button and to use a
>function inside the MenuButton class to just change
>the text of each button rather then swapping the set
>of buttons but am not having any luck there either.

Giving a button new text is just a matter of using the configure
method with the same argument you'd use when creating the object:

>>> from Tkinter import *
>>> r=Tk()
>>> b=Button(r,text="foo")
>>> b.pack()
>>> b.configure(text="bar")

Regards,
Matt



More information about the Python-list mailing list