Reputation: 23
I am wondering how I can change the coordinates and size of a button. I know you can do button1.pack(side=RIGHT)
but what if I want to say button1.pack(x=100, y=40)
. I have tried button1.geometry
but that hasn't worked.
Answer:
I did button1.place(x=0, y=0)
and the button went to the top corner.
Here is the code I used if anybody is curious:
from tkinter import *
t = Tk()
t.title("Testing")
t.geometry("250x250")
MyButton = Button(t, text="Click Me")
MyButton.pack()
def Clicked(event):
MyButton.place(x=0, y=0)
MyButton.bind("<Button-1>" ,Clicked)
MyButton.pack()
t.mainloop()
Upvotes: 2
Views: 4116
Reputation: 142641
Tkinter
has three layout managers
You can use x,y
with place()
but then rather you shouldn't use other managers which calculate position automatically and they may have problem when you put something manually using pack()
.
But you may put Frame
(using place()
) and use other manager inside Frame
pack()
and grid()
are more popular because they calculate positions automatically when you change size or you use on different system.
Example created few days ago for other question.
Button
moves Frame
in random position.
EDIT: now Button
moves itself to random position and changes height.
import tkinter as tk
import random
# --- function ---
def move():
#b.place_forget() # it seems I don't have to use it
# to hide/remove before I put in new place
new_x = random.randint(0, 100)
new_y = random.randint(0, 150)
b.place(x=new_x, y=new_y)
b['height'] = random.randint(1, 10)
# --- main ---
root = tk.Tk()
b = tk.Button(root, text='Move it', command=move)
b.place(x=0, y=0)
root.mainloop()
Upvotes: 1