【发布时间】:2018-09-04 23:01:38
【问题描述】:
在 Python 3.5.2 Tkinter 中,我正在创建一个基本的“菜单”系统,在该系统中,人们可以从菜单中订购一些东西,然后根据他们订购的价格在底部创建一个账单。到目前为止的代码如下:
from tkinter import *
root = Tk()
root.geometry("500x500")
text1 = Label(root, text="Menu", font='Verdana, 15')
text1.pack()
coststr = StringVar()
cost = 0
coststr.set(str(cost))
menu = ["Burger", "Chips", "Milkshake"]
textln = Label(root, text="\n")
textln.pack()
def choiceburger():
global cost
global coststr
cost += 1.99
coststr.set(str(cost))
def choicechips():
global cost
global coststr
cost += 1.49
coststr.set(str(cost))
def choicemilkshake():
global cost
global coststr
cost += 0.99
coststr.set(str(cost))
burgerbutton = Button(root, text=" Burger £1.99 ", command=choiceburger)
burgerbutton.pack()
chipsbutton = Button(root, text=" Chips £1.49 ", command=choicechips)
chipsbutton.pack()
milksbutton = Button(root, text=" Milkshake £0.99 ", command=choicemilkshake)
milksbutton.pack()
textln = Label(root, text="\n")
textln.pack()
textln = Label(root, text="\n")
textln.pack()
textln = Label(root, text="\n")
textln.pack()
textln = Label(root, text="\n")
textln.pack()
textln = Label(root, text="\n")
textln.pack()
costlabel = Label(root, textvariable=coststr, font='Verdana, 15')
costlabel.pack()
如您所见,单击按钮后,底部会保留一个数字,但没有任何货币符号(£ 或 $)。由于我制作了textvariable=coststr,因此我无法编辑标签以在成本前添加 £ 或 $ 符号。有没有办法做到这一点?它已经在python中定义了吗?谢谢
【问题讨论】:
-
使用 f 字符串格式化
coststr.set(str(cost)):coststr.set(f'{cost} {currency_symbol}')