0

Have a look at this code example:

from tkinter import *
root = Tk()

def createbuttons ():
    texts = ["Do this", "Do that", "Hide"]
    global btns
    btns = []
    for btn in texts:
        b = Button(root, text= btn, width=20)
        b.pack(side=LEFT, padx=15)
        btns.append(b)
    btns[2].config(command=hide)

def hide ():
    btns[0].pack_configure(padx=(15,105))
    btns[1].destroy()
    btns[2].pack_configure(padx=(105,15))

createbuttons()
root.mainloop()

Everything works fine for me, but it seems like a newbie solution to the problem. Since the button width is a mix of pixles and character width I didn't know how many pixles I needed to add, but after trying it like 10 times it looked good with 210 pixles (105 + 105).

My question is: is there a better way to do this? Or at least a way to know how many pixles a button takes up?

Thanks in advance!

Myone
  • 1,103
  • 2
  • 11
  • 24

1 Answers1

0

One option is to not delete your button, but draw something over it. hide body could become

f = Frame(root)
f.place(in_=btns[1], relwidth=1, relheight=1)

Here is another answer that illustrate how to use lower and lift https://stackoverflow.com/a/5928294 Another alternative would be to draw the frame in place of your buttons.


edited after comment of Bryan Oakley, original suggestion was:
f = Frame(root)
geometry = btns[1].winfo_geometry()
size,x,y = geometry.split("+")
width, height = size.split("x")
f.place(x=x, y=y, width=width, height=height)
Community
  • 1
  • 1
FabienAndre
  • 4,514
  • 25
  • 38