简体   繁体   中英

how can i make my buttons resize

Been having a right headache trying to get my buttons to resize with my window. I've tried all sorts of things but to no avail, I've managed to get the buttons to move position when resized but just can't seem to bind the buttons to the screen, I just get errors.

I have narrowed my script down to just one function so its easier to read, if somebody could point me in the right direction I would be much obliged.

Thanks

#!/usr/bin/python
from Tkinter import*

resizeTest = Tk()
resizeTest.title('Resize Test')
resizeTest.geometry('220x100')
menubar = Menu(resizeTest)
#----------view menu
def normal():
    resizeTest.geometry('220x100')
def enlarge():
    resizeTest.geometry('500x360')

viewMenu = Menu(menubar, tearoff = 0)    
viewMenu.add_command(label="Enlarge                Alt+1", command=enlarge)
viewMenu.add_command(label="Normal                 Alt+2", command=normal)
menubar.add_cascade(label="View", menu=viewMenu)
buttons = Frame(resizeTest, bd=0, width=5, height=1, relief=GROOVE)
buttons.grid(column=0, row=1, padx=1)    
numbers=["7", "4", "1", "8", "5", "2", "9", "6", "3"]
for index in range(9):
    n=numbers[index]
    Button(buttons, bg="White", text=n,width=5,height=1, relief=GROOVE).grid(padx=2,pady=2, row=index%3,column=index/3) 
resizeTest.config(menu=menubar)
resizeTest.mainloop()

Have you tried something like this? Adding a function to create the buttons, with width and height arguments, could be called in your resize functions to set up buttons to match the window size. Not exactly bound automatically, but it works.

#!/usr/bin/python
from Tkinter import*

resizeTest = Tk()
resizeTest.title('Resize Test')
resizeTest.geometry('220x100')
menubar = Menu(resizeTest)
#----------view menu
def normal():
    buttons.grid_forget()
    resizeTest.geometry('220x100')
    add_buttons(5, 1)
def enlarge():
    buttons.grid_forget()
    resizeTest.geometry('500x360')
    add_buttons(18, 6)

def add_buttons(w,h):
    global buttons
    buttons = Frame(resizeTest, bd=0, relief=GROOVE)
    buttons.grid(column=0, row=1, padx=1) 
    numbers=["7", "4", "1", "8", "5", "2", "9", "6", "3"]
    for index in range(9):
        n=numbers[index]
        Button(buttons, bg="White", text=n, width=w, height=h, relief=GROOVE).grid(padx=2,pady=2, row=index%3,column=index/3)   

viewMenu = Menu(menubar, tearoff = 0)    
viewMenu.add_command(label="Enlarge                Alt+1", command=enlarge)
viewMenu.add_command(label="Normal                 Alt+2", command=normal)
menubar.add_cascade(label="View", menu=viewMenu)
add_buttons(5, 1)
resizeTest.config(menu=menubar)
resizeTest.mainloop()

I've got it, had to change to the pack method but cracked it in the end, Thanks for your help

#!/usr/bin/python
from Tkinter import *
class resizeTest:
    def __init__( self, master ):
        def normal():
            self.ma.geometry('220x100')     
        def enlarge():
            self.ma.geometry('500x360')
        self.ma = master
        self.ma.title('Resize Test')
        self.ma.geometry('220x100')
        self.f = Frame( self.ma )
        self.f.pack(fill=BOTH, expand=YES)
        self.enlarge = Button( self.f, text='Enlarge', height=1, width=10, padx=0, pady=1, command=enlarge)
        self.enlarge.pack(side=LEFT, fill=BOTH,expand=YES)
        self.normal = Button( self.f, text='Normal', height=1, width=10, padx=0, pady=1, command=normal)
        self.normal.pack(side=RIGHT, fill=BOTH,expand=YES)
root = Tk()
app = resizeTest(root)
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