我使用 Tkinter Python 制作了一个桌面应用程序,但按钮的位置有问题,尤其是退出按钮,我使用“pack”,如下图所示:
我尝试更改窗口的默认大小,我在谷歌中搜索,发现所有解决方案都不使用“pack”
这是代码:
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()
您需要将
anchor="s"
添加到 .pack(...)
才能将按钮放在窗口底部:
buttonExit = Button(root, width=10, text="Exit", command=root.quit, bg='yellow')
buttonExit.pack(side=RIGHT, anchor="s")
另请注意,如果您想稍后引用
.pack(...)
,则需要在另一行中调用 buttonExit
。