[英]How to center labels and Entry fields in Tkinter Python?
我正在制作一個數據庫程序,其中提示用戶登錄屏幕,但我在下面有一個問題。
#Importing module
import tkinter as tk
from tkinter import *
#Window
root = tk.Tk()
root.geometry('400x120')
root.title('DATABASE PROTOTYPE V.1')
#Fullscreen Toggle
root.attributes('-fullscreen', True)
#Title Displayed
Label(root, text="DATABASE PROTOTYPE V.1",
font=('Helvetica 12 bold'),
background="yellow3").grid(row=250, column=250, pady=650, padx= 1005)
#Entry_Label
tk.Label(root, text="username:",font=('Times New Roman',20)).grid(pady=1,padx =5,row=2)
tk.Label(root,text="password:",font=('Times New Roman',20)).grid(pady=1,padx =5,row=1)
#Entry
e = tk.Entry(root,font=('April 16'))
en = tk.Entry(root,show = '*', font=('April 16'))
e.grid(row=0, column=1)
en.grid(row=1, column=1)
e.place(x=125,y=10)
en.place(x=125,y=47)
#Buttons
ButtonOne = tk.Button(text ="EXIT",font=('April 8'), command = root.destroy, pady= 1, padx= 1)
ButtonOne.place (x=360, y=90)
ButtonTwo = tk.Button(text ="INFO", font=('April 8'), command = root.destroy, pady= 1, padx= 1)
ButtonTwo.place (x=325, y=90)
ButtonThree = tk.Button(text ="ABOUT", font=('April 8'), command = root.destroy, pady= 1, padx= 1)
ButtonThree.place (x=275, y=90)
ButtonFour = tk.Button(text ="FORGOT CREDENTIALS?", font=('April 8'), command = root.destroy, pady= 1, padx= 1)
ButtonFour.place (x=140, y=90)
#Database
#End
root.mainloop()
當這段代碼專門運行時,條目的標簽一直在左上角,我該如何解決這個問題?
我建議您使用包幾何管理器而不是位置和網格。 Pack 會自動將您的小部件放在中心。 由於您有兩個並排的小部件(標簽和條目),您可以使用 side 選項並指定 side= LEFT 或 RIGHT。 Pack 還為您提供了根據可用空間操作小部件的選項。 即擴展和填充。
查看此鏈接以了解更多信息... https://www.pythontutorial.net/tkinter/tkinter-pack/
編輯:更新的行號。
我在第 6 行和第 15 行進行了更改。在第 10 行注釋掉。
在第 15 行中不要將值設置得太高row=250, column=250, pady=650, padx= 1005)
import tkinter as tk
from tkinter import *
#Window
root = tk.Tk()
root.geometry('400x200')
root.title('DATABASE PROTOTYPE V.1')
#Fullscreen Toggle
#root.attributes('-fullscreen', True)
#Title Displayed
Label(root, text="DATABASE PROTOTYPE V.1",
font=('Helvetica 12 bold'),
background="yellow3").grid(row=50, column=50, pady=50, padx= 5)
#Entry_Label
tk.Label(root, text="username:",font=('Times New Roman',20)).grid(pady=1,padx =5,row=2)
tk.Label(root,text="password:",font=('Times New Roman',20)).grid(pady=1,padx =5,row=1)
#Entry
e = tk.Entry(root,font=('April 16'))
en = tk.Entry(root,show = '*', font=('April 16'))
e.grid(row=0, column=1)
en.grid(row=1, column=1)
e.place(x=125,y=10)
en.place(x=125,y=47)
#Buttons
ButtonOne = tk.Button(text ="EXIT",font=('April 8'), command = root.destroy, pady= 1, padx= 1)
ButtonOne.place (x=360, y=90)
ButtonTwo = tk.Button(text ="INFO", font=('April 8'), command = root.destroy, pady= 1, padx= 1)
ButtonTwo.place (x=325, y=90)
ButtonThree = tk.Button(text ="ABOUT", font=('April 8'), command = root.destroy, pady= 1, padx= 1)
ButtonThree.place (x=275, y=90)
ButtonFour = tk.Button(text ="FORGOT CREDENTIALS?", font=('April 8'), command = root.destroy, pady= 1, padx= 1)
ButtonFour.place (x=140, y=90)
#Database
#End
root.mainloop()
Output:
如果你想讓你的 GUI響應式,最好使用.grid()
或.pack()
而不是.place()
。 此外,為了更容易布局您的 GUI,將相關的小部件分組到框架中。
建議將標簽、條目和按鈕放在一個框架中,因為將框架放在中心更容易。
下面是修改后的代碼:
#Importing module
import tkinter as tk
#Window
root = tk.Tk()
#root.geometry('400x200')
root.title('DATABASE PROTOTYPE V.1')
#Fullscreen Toggle
#root.attributes('-fullscreen', True)
#Title Displayed at the bottom-right corner
tk.Label(root, text="DATABASE PROTOTYPE V.1", font=('Helvetica 12 bold'),
background="yellow3").pack(side=tk.BOTTOM, anchor='e')
input_frame = tk.Frame(root, padx=50, pady=50)
input_frame.pack(expand=1) # make the frame at the center of its parent
#Entry_Label
tk.Label(input_frame, text="username:", font=('Times New Roman',20)).grid(pady=1, padx=5, row=0)
tk.Label(input_frame, text="password:", font=('Times New Roman',20)).grid(pady=1, padx=5, row=1)
#Entry
e = tk.Entry(input_frame, font=('April 16'))
en = tk.Entry(input_frame, show='*', font=('April 16'))
e.grid(row=0, column=1)
en.grid(row=1, column=1)
# frame for those buttons
button_frame = tk.Frame(input_frame)
button_frame.grid(columnspan=2, sticky='e')
#Buttons
ButtonOne = tk.Button(button_frame, text="EXIT",font=('April 8'),
command=root.destroy, pady=1, padx=1)
ButtonOne.pack(side='left')
ButtonTwo = tk.Button(button_frame, text="INFO", font=('April 8'),
command=root.destroy, pady=1, padx=1)
ButtonTwo.pack(side='left')
ButtonThree = tk.Button(button_frame, text="ABOUT", font=('April 8'),
command=root.destroy, pady=1, padx=1)
ButtonThree.pack(side='left')
ButtonFour = tk.Button(button_frame, text="FORGOT CREDENTIALS?", font=('April 8'),
command=root.destroy, pady=1, padx=1)
ButtonFour.pack(side='left')
#Database
#End
root.mainloop()
結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.