I've a probleme with tkinter, with the pady

Question:

Here is my code: (sorry it’s in french)

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()

error message :

Traceback (most recent call last):
  File "C:UsersjeremDesktopnsiinterfarce.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 FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0libtkinter__init__.py", line 3187, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0libtkinter__init__.py", line 2601, in __init__
    self.tk.call(
_tkinter.TclError: bad screen distance "50 0"

I try to put a pady in a Label and that not working due to the pady=(50, 0) but i don’t know why it’s not working

It works with pady=50 but I don’t want to use that

Asked By: Jeremiel

||

Answers:

The pady (and padx) options of a Label only accept a single value, not a tuple. That is what bad screen distance "50 0" is trying to tell you.

You can use a tuple when specifying a padding value for the pack and grid commands, but not for the padding associated with a specific widget.

Answered By: Bryan Oakley
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.