Search code examples
pythontkintertkinter-layouttkinter-button

I want a tkinter button in python to change its position by resizing the window


I made a desktop app using Tkinter Python and I had a problem with the positions of the button, especially the exit button, I am using "pack" as shown in the following imgs:

the right position

Here is the problem

I've tried to change the default size of the window and I searched in google and I find all solutions aren't using "pack"

Here is the code:

from tkinter import *
from deep_translator import GoogleTranslator

root = Tk()
root.title("Marwan Translator")
root.iconbitmap("E:\Marwan\PROGRAMMING\pyProjects\#11 Marwan Translator\icon -.ico")

#Translation Function
def f_translation():
        try:
                global translated_text 
                translated_text = GoogleTranslator(source=t_from.get(),target=t_to.get()).translate(t_text.get())
                l_translation = Label(root, text=translated_text,fg="orange",font=('Times New Roman',15)).pack()
                
                
                

        except:
                e_except =Label(root,text="The language is not correct, Please enter the correct language",
                                font=('Times New Roman',15)).pack()

        


#Welcoming
e_title = Label(text="""Welcome To the best Translator
        MarwanTranslator          """, 
                bg= 'light blue',
                fg='red',
                borderwidth=10,
                width=40,
                height=3,
                font=('Times New Roman',25)).pack()
#Enter the languages
e_lang = Label(text="Please! Enter The languages", 
                fg='blue',
                bg='light green',
                borderwidth=10,
                width=30,
                height=1,
                font=('Times New Roman',10)).pack()

#The from language
t_fro = Label(text="From:",fg="purple",font=('Times New Roman',18)).pack()
t_from = Entry(root,width=10,borderwidth=3)
t_from.pack()

#The to language
t_too = Label(text="To:",fg="purple",font=('Times New Roman',18)).pack()
t_to = Entry(root,width=10,borderwidth=3)
t_to.pack()


#Take the text
e_text = Label(root,text="Please! Enter The Text:",fg="green",font=('Times New Roman',15)).pack()
t_text = Entry(root, width=70, borderwidth=6)
t_text.pack()


#Translated button
b_translate = Button(root,
                     text="Translate..",
                     padx=10, pady=5,
                     font=('Times New Roman',10),fg="red",bg="light blue",
                     command=f_translation).pack()

#Returning the translated text
e_translated = Label(root, text="Here Is The Translation:",fg="blue",font=('Times New Roman',15)).pack()

#Exit Button
buttonExit = Button(root,width=10,text="Exit",command=root.quit,bg='yellow').pack(side=RIGHT)
root.mainloop()

Solution

  • You need to add anchor="s" to .pack(...) in order to put the button at the bottom of the window:

    buttonExit = Button(root, width=10, text="Exit", command=root.quit, bg='yellow')
    buttonExit.pack(side=RIGHT, anchor="s")
    

    Note also that you need to call .pack(...) in another line if you want to reference buttonExit later.