我不知道如何制作一个输出整数的按钮。
这是一个收银机系统,每次用户单击该按钮时,价格应自动进行总计,并自动减去并显示更改。
from tkinter import *
from tkinter.font import Font
class MainUI:
def __init__(self,master):
#MAIN WINDOW
self.mainFrame = Frame(master, width=800, height=600,
bg="skyblue")
master.title("FCPC CASH REGISTER")
self.mainFrame.pack()
self.title = Label(self.mainFrame, text="FIRST CITY PROVIDENTIAL"
" COLLEGE", bg="blue")
self.font = Font(family="Helvetica", size=29)
self.title.configure(font=self.font)
self.title.place(x=50, y=50)
#BUTTONS
self.snacksButton = Button(self.mainFrame, text="SNACKS",
command=self.snackList, width=10, bg="blue")
self.font = Font(family="Helvetica", size=20)
self.snacksButton.configure(font=self.font)
self.snacksButton.place(x=100,y=150,)
#TOTAL
self.total = Label(self.mainFrame,text="TOTAL:",
bg="blue",width=8)
self.total.place(x=370,y=160)
self.font = Font(family="Helvetica", size=25)
self.total.configure(font=self.font)
#CHANGE
self.change = Label(self.mainFrame,text="CHANGE:", bg="blue")
self.change.place(x=370,y=240)
self.font = Font(family="Helvetica", size=25)
self.change.configure(font=self.font)
def snackList(self):
self.frame = Toplevel(width=800,height=600,bg="skyblue")
self.frame.title("SNACKS")
self.novaButton = Button(self.frame, text="NOVA(12php)",
command=self.calculate ,width=15, bg="blue")
self.font = Font(family="Helvetica", size=15)
self.novaButton.configure(font=self.font)
self.novaButton.place(x=100,y=150)
root = Tk()
ui = MainUI(root)
root.mainloop()
我希望按钮输出价格并自动计算总金额。
答案 0 :(得分:0)
我整理了一个简单的示例来说明您可能需要做的事情。将每个按钮与一个功能关联起来,该功能将零食的价格加到总量上并更改显示的内容。一种方法是使用tkinter Label小部件的textvariable选项。
示例:
import tkinter as tk
def calculate(price):
global total, var
total += price
text = 'Total = ' + str(total)
var.set(text)
root = tk.Tk()
total = 0
var = tk.StringVar()
button1 = tk.Button(root, text='Snickers 1$', command=lambda: calculate(price=1)).pack()
button2 = tk.Button(root, text='Mars 2$', command=lambda: calculate(price=2)).pack()
totalLabel = tk.Label(root, textvariable=var).pack()
root.mainloop()