1.11M Members

Tkinter & button placement problem

 
0
 

Hi. I'm creating an interface using the Tkinter library, but I seem to be having some issues with placing 3 buttons BELOW 3 buttons which are on top. All buttons should be inside the second LabelFrame. I've looked all over and I can't seem to figure out why the other buttons are not placing themselves below the top 3. I've tried various options using the pack method such as side = BOTTOM or anchor = SW, but they don't seem to be helping at all.

from Tkinter import *

class GUI():
    def __init__(self, r):
        self.r = r
        
        self.i = StringVar()
        
        self.f = Frame(self.r)
        self.f.pack(fill = BOTH)
        
        self.g = LabelFrame(self.f, text = 'Groupbox 1', height = 90, width = 150, font = 'Lucida 8')
        self.g.pack_propagate(0)
        self.g.pack(side = LEFT, pady = 10, padx = 10)
        
        self.id = Entry(self.g, justify = CENTER, textvariable = self.i, font = 'Lucida 8')
        self.id.pack(pady = 22)
        
        self.opt = LabelFrame(self.f, text = 'Groupbox 2', height = 90, width = 200, font = 'Lucida 8')
        self.opt.pack_propagate(0)
        self.opt.pack(side = LEFT)
        
        self.button1 = Button(self.opt, text = 'Button 1', height = 1, width = 7, font = 'Lucida 8')
        self.button1.pack(side = LEFT, anchor = N, pady = 5, padx = 4)
        
        self.button2 = Button(self.opt, text = 'Button 2', height = 1, width = 7, font = 'Lucida 8')
        self.button2.pack(side = LEFT, anchor = N, pady = 5, padx = 4)
    
        self.button3 = Button(self.opt, text = 'Button 3', height = 1, width = 7, font = 'Lucida 8')
        self.button3.pack(side = LEFT, anchor = N, pady = 5, padx = 4)
        
        
        self.button4 = Button(self.opt, text = 'Button 4', height = 1, width = 5, font = 'Lucida 8')
        self.button4.pack(side = BOTTOM, anchor = W)
        
        # Buttons 5 & 6 will be added, but I'd first like to rectify button 4's issue.

t = Tk()
t.title('test')
#t.configure(bg = 'white')
t.geometry('400x120')
t.pack_propagate(0)

GUI(t)
t.mainloop()

As stated, I've changed the options in the button4's pack method, but it doesn't seem to want to place itself directly beneath the other buttons. It's placing itself to the right. I'd like to stay away from the grid manager, as I've already gotten most of what I need in the right place.

I'd appreciate any help in this situation, and hopefully it wasn't a simple mistake on my part.

 
1
 

You either use the grid manager or a separate Frame for each set of 3 buttons.

from Tkinter import *
 
class GUI():
    def __init__(self, r):
        self.r = r
 
        self.i = StringVar()
 
        self.f = Frame(self.r)
        self.f.pack(fill = BOTH)
 
        self.g = LabelFrame(self.f, text = 'Groupbox 1', height = 90, width = 150, font = 'Lucida 8')
        self.g.pack_propagate(0)
        self.g.pack(side = LEFT, pady = 10, padx = 10)
 
        self.id = Entry(self.g, justify = CENTER, textvariable = self.i, font = 'Lucida 8')
        self.id.pack(pady = 22)
 
        self.opt = LabelFrame(self.f, text = 'Groupbox 2', height = 90, width = 200, font = 'Lucida 8')
        self.opt.pack_propagate(0)
        self.opt.pack(side = TOP)
 
        f1 = Frame(self.opt)
        for ctr in range(3):
            but = Button(f1, text = 'Button '+str(ctr+1), height=1, width=5, font='Lucida 8')
            but.pack(side = LEFT, anchor = N, pady = 5, padx = 4)
        f1.pack(side=TOP) 
 
 
        f2 = Frame(self.opt, height = 50, width = 200)
        for ctr in range(3):
            but = Button(f2, text = 'Button '+str(ctr+4), height=1, width=5, font='Lucida 8')
            but.pack(side = LEFT, anchor = N, pady = 5, padx = 4)
        f2.pack(side=BOTTOM)

t = Tk()
t.title('test')
#t.configure(bg = 'white')
t.geometry('400x120')
t.pack_propagate(0)
 
GUI(t)
t.mainloop()
 
0
 

That's exactly what I've been looking for actually. Thank you so much.

I feel somewhat idiotic for not thinking of that in the first place, but I'll keep it in mind about making another frame in future projects.

Thank you again. :3

 
0
 

Hello init

I just don't know why you don't work with .place() in place of .grid() , or .pack() , with .place( x= , y= ) , u can place where u want your objects in a window or a frame .. good luck .

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: