Skip to content Skip to sidebar Skip to footer

How Can I Get Rid Of Curly Braces When Using White Space In Python With Tkinter?

I am trying to write a tkinter program that prints out times tables. To do this, I have to edit a text widget to put the answer on the screen. All of the sums come up right next to

Solution 1:

To get each equation on its own line, you might want to build the whole table as one string:

table = ',\n'.join(['{w} x {h} = {a}'.format(w=whichtable, h=h, a=whichtable*h)
                   for h in range(howfar,0,-1)])
answer.insert("1.0", table)

Also, if you add fill and expand parameters to answer.pack, you will be able to see more of the table:

answer.pack(fill="y", expand=True)

Solution 2:

In line 15, you set "text" to a tuple of mixed ints and strings. The widget is expecting a string, and Python converts it oddly. Change that line to build the string yourself:

text = " ".join((str(whichtable), "x", str(howfar), "=", str(howfar*whichtable), ", "))

Solution 3:

In line 15, use .format() to format your text:

'{} x {} = {},'.format(whichtable, howfar, howfar * whichtable)

According to the docs:

This method of string formatting is the new standard in Python 3, and should be preferred to the % formatting described in String Formatting Operations in new code.

Post a Comment for "How Can I Get Rid Of Curly Braces When Using White Space In Python With Tkinter?"