Trouble with Tk buttons

David AraSmith mycerius at yahoo.com
Tue Aug 21 13:24:24 EDT 2001


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:

from Tkinter import *

class MainFrame(Frame):
   def __init__(self, master=None, **kw):
      Frame.__init__(self, master, **kw)
      self.menuFrame = MenuFrame(self, bd=2,
relief='ridge')
      self.menuFrame.pack(side=TOP, fill=X)
      self.basicFrame = BasicFrame(self, bd=0)
      self.basicFrame.pack(side=BOTTOM)
      self.buttonsFrame = ButtonsFrame(self, bd=2)
   def setBasic(self):
      self.buttonsFrame.pack_forget()
      self.basicFrame.pack(side=BOTTOM)
   def setButtons(self):
      self.basicFrame.pack_forget()
      self.buttonsFrame.pack(side=BOTTOM)

class MenuFrame(Frame):
   def __init__(self, master=None, **kw):
      Frame.__init__(self, master, **kw)
      MenuButton(self, text='Basic',
command=master.setBasic).pack(side=LEFT)
      MenuButton(self, text='Buttons',
command=master.setButtons).pack(side=LEFT)

class MenuButton(Button):
   def __init__(self, parent=None, **kw):
      Button.__init__(self, parent, **kw)
   def setText(self, cat):
      self.configure(text=cat)
		
class BasicFrame(Frame):
   def __init__(self, master=None, **kw):
      Frame.__init__(self, master, **kw)
      Label(self, text='Name:').grid(row=0, column=0)

class ButtonsFrame(Frame):
   def __init__(self, master=None, **kw):
      Frame.__init__(self, master, **kw)
      MenuButton(self, text='Button1',
command=self.setButton1).grid(row=0, column=0)
      MenuButton(self, text='Button2',
command=self.setButton2).grid(row=0, column=1)
   def setButton1(self):
      self.setSpecificBut('A1', 'A2')
   def setButton2(self):
      self.setSpecificBut('Button1', 'Button2')
   def setSpecificBut(self, but1, but2):
      MenuButton(self, text=but1,
command=self.setButton1).grid(row=1, column=0)
      MenuButton(self, text=but2,
command=self.setButton2).grid(row=1, column=1)

root = Tk()

root.title("Temp testing")
root.config(height=200, width=300)

frame1 = MainFrame(root, bd=0, relief="flat")
frame1.pack()

root.mainloop()

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.
Thanks in advance.

Dave


__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/




More information about the Python-list mailing list