使用 tkinter 在 python 中使用空格时,如何去掉花括号? [英] How can I get rid of curly braces when using white space in python with tkinter?

查看:36
本文介绍了使用 tkinter 在 python 中使用空格时,如何去掉花括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个打印时间表的 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 each other with no spaces, when I add a space between them, curly braces appear around my white space. How ca I get rid of those curly braces?

附言这是我的代码:

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

推荐答案

为了让每个方程独立于一行,您可能希望将整个表格构建为一个字符串:

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)

<小时>

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


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)

这篇关于使用 tkinter 在 python 中使用空格时,如何去掉花括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆