You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Tkinter中如何让登录界面的标签与Entry输入框居中?

修复Tkinter登录界面标签位置异常问题

你的登录界面标签全部跑到屏幕左上角,核心问题是同时混用了grid和place两种布局管理器,加上标签的行号设置混乱,导致布局逻辑冲突。以下是具体修复方案:

修复要点

  • 不要同时使用grid和place,选择一种布局管理器统一布局,这里推荐用grid实现表单式规整布局
  • 让标签和对应的输入框处于同一行,保证布局对应关系正确
  • 修正标题的布局参数,避免用过大的row、padx、pady值导致位置异常
  • 按钮可根据需求调整位置,适配全屏窗口

修复后的完整代码

# 导入模块
import tkinter as tk
from tkinter import *

# 窗口设置
root = tk.Tk()  
root.title('DATABASE PROTOTYPE V.1')
# 全屏设置
root.attributes('-fullscreen', True)

# 标题显示
title_label = Label(root, text="DATABASE PROTOTYPE V.1",
                    font=('Helvetica 12 bold'),
                    background="yellow3")
title_label.grid(row=0, column=0, columnspan=2, pady=30)

# 输入框标签与输入框布局(统一用grid)
# 用户名行
tk.Label(root, text="username:", font=('Times New Roman',20)).grid(row=1, column=0, padx=20, pady=10, sticky='e')
e = tk.Entry(root, font=('Arial 16'))
e.grid(row=1, column=1, pady=10)

# 密码行
tk.Label(root, text="password:", font=('Times New Roman',20)).grid(row=2, column=0, padx=20, pady=10, sticky='e')
en = tk.Entry(root, show='*', font=('Arial 16'))
en.grid(row=2, column=1, pady=10)

# 按钮布局
button_frame = tk.Frame(root)
button_frame.grid(row=3, column=0, columnspan=2, pady=20)

ButtonFour = tk.Button(button_frame, text="FORGOT CREDENTIALS?", font=('Arial 8'), command=root.destroy)
ButtonFour.pack(side='left', padx=5)

ButtonThree = tk.Button(button_frame, text="ABOUT", font=('Arial 8'), command=root.destroy)
ButtonThree.pack(side='left', padx=5)

ButtonTwo = tk.Button(button_frame, text="INFO", font=('Arial 8'), command=root.destroy)
ButtonTwo.pack(side='left', padx=5)

ButtonOne = tk.Button(button_frame, text="EXIT", font=('Arial 8'), command=root.destroy)
ButtonOne.pack(side='left', padx=5)

# 数据库逻辑(后续补充)

root.mainloop()

说明

  • 用grid统一布局,标签和输入框的row一一对应,sticky='e'让标签靠右对齐,和输入框形成整齐的表单样式
  • 标题用columnspan=2跨列显示,保证居中效果
  • 按钮放入一个Frame中,用pack排列,避免布局混乱
  • 替换了不存在的字体April为系统通用的Arial,避免字体报错

内容的提问来源于stack exchange,提问作者Luntae

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 05:40:53