0

I am creating a form using Tkinter in Python and some of the labels have curly brackets ({ and }) around the text. Since the text is generated using a for loop from a dictionary and the iteration is identical for each label, I am unable to understand why some of the labels such {First Name} include curly brackets while others like City do not.

I am using Python 3.7.2 on Windows 10.

output of form

Code:

import tkinter as tk

window = tk.Tk()
window.title("Address Entry Form")

frame1 = tk.Frame(master=window)
frame1["relief"] = tk.SUNKEN
frame1["borderwidth"] = 3
frame1.pack(fill=tk.X)

frame2 = tk.Frame(master=window)
frame2.pack(fill=tk.X)

list = {
    0: "First Name",
    1: "Last Name",
    2: "Address Line 1",
    3: "Address Line 2",
    4: "City",
    5: "State/Province",
    6: "Postal Code",
    7: "Country",
}

for i in list:
    label = tk.Label(master=frame1)
    label["text"] = list[i], ":"
    label.grid(row=i, column=0, sticky=tk.E)

    entry = tk.Entry(master=frame1)
    entry["width"] = 50
    entry.grid(row=i, column=1)

button1 = tk.Button(master=frame2)
button1["text"] = "Submit"
button1["width"] = 7
button1["height"] = 1
button1.pack(padx=5, pady=5, side=tk.RIGHT)

button2 = tk.Button(master=frame2)
button2["text"] = "Clear"
button2["width"] = 5
button2["height"] = 1
button2.pack(padx=5, pady=5, side=tk.RIGHT)

window.mainloop()
1
  • 2
    per the duplicate, your label is a tuple - list[i],":". Make it a string like '{}:'.format(list[i]) Commented Feb 17, 2019 at 23:09

1 Answer 1

-1

In line 29,

label["text"] = list[i],":"

change the comma to a +

label["text"] = list[i] + ":"

not sure why this works, but it does.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.