简体   繁体   中英

center many widgets in window [tkinter]

I need to center two buttons, and I may need to center more buttons, but I can't center more than one button, so I need help...

Here's the code:

from tkinter import *
from tkinter.ttk import * 
import os

root = Tk()
root.geometry("325x100")
    
def click():
    pass
        
def click2():
    pass
            
button = Button(root, text="Button 1", command=click, width=25)
button.grid(row=0, column=0)

button2 = Button(root, text="Button 2", command=click2, width=25)
button2.grid(row=1, column=0)
      
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
      
root.mainloop()

I did a bit of testing and here is what i have come up with. I have used the.pack() method instead of the.grid() method and i have also used a frame. I am kind of new to Python, but here it is:)

from tkinter import *
from tkinter.ttk import *
import os

root = Tk()
root.geometry("325x100")

def click():
    pass

def click2():
    pass

frame = Frame(root)
frame.pack(padx = 20, pady = 12)

button = Button(root, text="Button 1", command=click, width=25)
button.pack()

button2 = Button(root, text="Button 2", command=click2, width=25)
button2.pack()


root.mainloop()

Here is what it looks like:

在此处输入图像描述

Don't add weight to the first row. It is forcing it to expand. You may want to consider something else, though. You may eventually put something else on that row, and you may need that thing to expand the row. In it's current state this will cause a "catch 22" for you. You may want to consider creating a frame to hold all of the buttons, and put that entire frame on the root.


immediate fix:

from tkinter import *
from tkinter.ttk import * 
import os

root = Tk()
root.geometry("325x100")
    
def click():
    pass
        
def click2():
    pass
            
button = Button(root, text="Button 1", command=click, width=25)
button.grid(row=0, column=0)

button2 = Button(root, text="Button 2", command=click2, width=25)
button2.grid(row=1, column=0)

#this is forcing the top row to expand     
#root.grid_rowconfigure(0, weight=1)   
root.grid_columnconfigure(0, weight=1)
      
root.mainloop() 

possibly a better way:

from tkinter import *
from tkinter.ttk import * 
import os

root = Tk()
root.geometry("325x100")
    
def click():
    pass
        
def click2():
    pass
    
#by not defining row and column in grid() 
#~ row will be the next available one and column will be 0
    
button_frame = Frame(root)
button_frame.grid(sticky='nswe')
button_frame.grid_columnconfigure(0, weight=1)

#you only need to store a reference if you intend to change/reference/destroy/forget these
#if they are going to always be a button, as initially defined, a reference is dead weight          
Button(button_frame, text="Button 1", command=click, width=25).grid()
Button(button_frame, text="Button 2", command=click2, width=25).grid()

#now you can use grid_rowconfigure without it destroying your button layout      
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
      
root.mainloop() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM