0

我正在尝试编写一个打印出时间表的 tkinter 程序。为此,我必须编辑一个文本小部件以将答案显示在屏幕上。所有的总和都紧挨着没有空格,当我在它们之间添加一个空格时,我的空格周围会出现花括号。我怎样才能摆脱那些花括号?

PS这是我的代码:

#############
# Times Tables
#############

# Imported Libraries
from tkinter import *

# Functions
def function ():
    whichtable = int(tableentry.get())
    howfar = int(howfarentry.get())
    a = 1
    answer.delete("1.0",END)
    while a <= howfar:
        text = (whichtable, "x", howfar, "=", howfar*whichtable, ", ")
        answer.insert("1.0", text)
        howfar = howfar - 1

# Window
root = Tk ()

# Title Label
title = Label (root, text="Welcome to TimesTables.py", font="Ubuntu")
title.pack ()

# Which Table Label
tablelabel = Label (root, text="Which Times Table would you like to use?")
tablelabel.pack (anchor="w")

# Which Table Entry
tableentry = Entry (root, textvariable=StringVar)
tableentry.pack ()


# How Far Label
howfarlabel = Label (root, text="How far would you like to go in that times table?")
howfarlabel.pack (anchor="w")

# How Far Entry
howfarentry = Entry (root, textvariable=StringVar)
howfarentry.pack ()

# Go Button
go = Button (root, text="Go", bg="green", width="40", command=function)
go.pack ()

# Answer Text
answer = Text (root, bg="cyan", height="3", width="32", font="Ubuntu")
answer.pack ()

# Loop
root.mainloop ()
4

3 回答 3

1

要将每个方程放在自己的行上,您可能希望将整个表构建为一个字符串:

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)

此外,如果您将fillexpand参数添加到answer.pack,您将能够看到更多的表格:

answer.pack(fill="y", expand=True)
于 2013-04-20T11:55:54.667 回答
0

在第 15 行,用于.format()格式化您的文本:

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

根据文档:

这种字符串格式化方法是 Python 3 中的新标准,应该优先于新代码中字符串格式化操作中描述的 % 格式化。

于 2013-04-20T11:51:45.543 回答
0

在第 15 行,您将“text”设置为混合整数和字符串的元组。小部件需要一个字符串,而 Python 奇怪地转换了它。更改该行以自己构建字符串:

text = " ".join((str(whichtable), "x", str(howfar), "=", str(howfar*whichtable), ", "))
于 2013-04-20T11:43:51.750 回答