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

Tkinter中Label设置pady=(50,0)报错的问题求助

Tkinter Label组件设置上下不同内边距报错的解决方法

问题

用Tkinter开发GUI界面时,尝试为Label组件初始化时设置pady=(50, 0)来分别控制上下内边距,触发_tkinter.TclError错误,提示bad screen distance "50 0"。但设置pady=50时程序可正常运行,需要实现上下内边距分开设置的需求。

原代码

from tkinter import *

# creer la fenetre
window = Tk()
window.title("Interface projet-NSI vacances noel")
window.geometry("1080x720")
window.iconbitmap("ricardo.ico")
window.config(background='#4065A4')

# creer la frame principale
frame = Frame(window, background="#4065A4")

# creer des sous boites
top_left_frame = Frame(window, background="#4065A4")

# creer un titre1
label_title1 = Label(top_left_frame, text="Titre1", font=("Helvetica", 20), background="#4065A4", foreground="white", padx=10, pady=(50, 0))
label_title1.pack()

# creer boutton1
generate_password_button = Button(top_left_frame, text="Boutton1", font=("Helvetica", 20), background="#4065A4", foreground="white")
generate_password_button.pack(fill=X, padx=10, pady=5)

# on place la sous boite à droite de la frame principal
top_left_frame.grid(row=1, column=0, sticky=NE)

# afficher la frame
frame.grid()

# afficher la fenetre
window.mainloop()

错误信息

Traceback (most recent call last):
  File "C:\Users\jerem\Desktop\nsi\interfarce.py", line 17, in <module>
    label_title1 = Label(top_left_frame, text="Titre1", font=("Helvetica", 20), background="#4065A4", foreground="white", padx=10, pady=(50, 0))
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 3187, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 2601, in __init__
    self.tk.call(
_tkinter.TclError: bad screen distance "50 0"

解决方法

Tkinter组件初始化时的pady/padx参数只支持单个数值(上下/左右内边距相同),元组格式(上下/左右不同)仅在布局方法(pack()/grid()/place())中生效。

修改方式:将pady=(50, 0)从Label的初始化参数中移除,放到pack()方法内:

修改后的关键代码

# creer un titre1
label_title1 = Label(top_left_frame, text="Titre1", font=("Helvetica", 20), background="#4065A4", foreground="white", padx=10)
label_title1.pack(pady=(50, 0))  # 这里设置上下不同内边距

完整修改后代码

from tkinter import *

# creer la fenetre
window = Tk()
window.title("Interface projet-NSI vacances noel")
window.geometry("1080x720")
window.iconbitmap("ricardo.ico")
window.config(background='#4065A4')

# creer la frame principale
frame = Frame(window, background="#4065A4")

# creer des sous boites
top_left_frame = Frame(window, background="#4065A4")

# creer un titre1
label_title1 = Label(top_left_frame, text="Titre1", font=("Helvetica", 20), background="#4065A4", foreground="white", padx=10)
label_title1.pack(pady=(50, 0))

# creer boutton1
generate_password_button = Button(top_left_frame, text="Boutton1", font=("Helvetica", 20), background="#4065A4", foreground="white")
generate_password_button.pack(fill=X, padx=10, pady=5)

# on place la sous boite à droite de la frame principal
top_left_frame.grid(row=1, column=0, sticky=NE)

# afficher la frame
frame.grid()

# afficher la fenetre
window.mainloop()

这样修改后即可实现Label上下内边距分别为50和0的效果,且不会触发错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 06:00:46