python - tkinter 从表中乘以价格
问题描述
我正在为咖啡馆管理系统制作一个简单的程序,该程序将食物名称作为按钮从桌子上读取,我还想从桌子上读取食物价格并将其乘以按钮点击次数,然后将它们保存到Text
我可以请这样做
from tkinter import *
from tkinter import ttk
import sqlite3
import time
import datetime
import random
variable = 1
bttn_clicks=0
button_dict = {}
button_dic = {}
conn = sqlite3.connect('hoteldb.db')
c = conn.cursor()
def showqurec():
global button,data2
data2 = readqurec()
for index, dat in enumerate(data2):
button= ttk.Button(master, text=dat[0],command=lambda dat=dat:update_count(dat))
button.grid(row=index+1, column=0,padx=0, pady=0)
button_dict[dat] = button
def showqado():
global button,data2
data2 = readqado()
for index, dat in enumerate(data2):
button = ttk.Button(master, text=dat[0],command=lambda dat=dat: update_count(dat))
button.grid(row=index+1, column=1,pady=0,padx=0)
button_dict[dat] = button
def showcasho():
global button,data2
data2 = readcasho()
for index, dat in enumerate(data2):
button=ttk.Button(master, text=dat[0],command=lambda dat=dat:update_count(dat))
button.grid(row=index+1, column=2,padx=0, pady=0)
button_dict[dat] = button
def readfromdatabase():
cur.execute("SELECT qureec.name,qado.name,casho.name FROM qureec,qado,casho")
return cur.fetchall()
def readqurec():
cur.execute("SELECT name FROM qureec ")
return cur.fetchall()
def readqado():
cur.execute("SELECT name FROM qado ")
return cur.fetchall()
def readcasho():
cur.execute("SELECT name FROM casho ")
return cur.fetchall()
def update_count(x):
global bttn_clicks,my_text,price
my_text=StringVar()
for name in data2:
my_text = button_dict[x].cget('text')
bttn_clicks += 1
def Receipt():
txtReceipt.delete("1.0", "4.0")
x = random.randint(10908, 500876)
randomRef = str(x)
Receipt_Ref.set("BILL" + randomRef)
txtReceipt.insert("1.0", 'Receipt Ref: \t\t\t'+Receipt_Ref.get()+"\t\t"+DateofOrder.get()+"\n")
txtReceipt.insert("2.0", 'Items\t\t'+'Quantity\t\t\t'+"Price \n\n")
if variable !=0:
txtReceipt.insert(END, str(my_text)+'\t\t'+str(bttn_clicks)+'\t\t\t'+""+str(bttn_clicks*3)+"\n")
master=Tk()
master.geometry('630x350+100+200')
master.title('Records')
Label = Button(master, text="meal", width=10,command=showqurec)
Label.grid(row=0, column=0)
BMILabel = Button(master, text="tea", width=10,command=showqado)
BMILabel.grid(row=0, column=1)
stateLabel = Button(master, text="fast food", width=10,command=showcasho)
stateLabel.grid(row=0, column=2)
lblReceipt = Button(master,text="Get Receipt:",anchor='w',command=Receipt)
lblReceipt.grid(row=0,column=4,sticky=W)
txtReceipt = Text(master,bg="white",width=47,height=17.5)
txtReceipt.grid(row=1,column=4,rowspan=4,columnspan=40)
Receipt_Ref=StringVar()
DateofOrder = StringVar()
master.mainloop()
现在我宣布了价格,但是我如何从数据库中获取它们我还有另一个按钮点击在每个按钮上增加点击指定按钮的方式
解决方案
获取按钮上的名称而不仅仅是第一个字母,您必须将名称设置为整个字符串,而不仅仅是第一个位置:
button = ttk.Button(master, text=dat, command ...
(instead of dat[0])
每个类别:膳食、茶和快餐。
我没有清楚地了解您最终希望它如何工作。您是希望为每个产品更新收据还是仅在最后更新收据。该功能update_count()
看起来很奇怪。
我建议使用字典来存储产品和价格。这将使价格查询变得容易。只要应用程序相当小,我认为全局变量将是可取的。
products = {'Soup': 2.40, 'Fish': 3.10, 'Beef': 3.55, 'Pizza': 2.70}
看看下面的示例代码,尝试运行它并考虑您希望其余部分如何工作。每次按下按钮时都应该更新收据吗?是否应该以任何方式订购收据?如果你想从收据中删除一些东西怎么办?
from tkinter import *
from tkinter import ttk
#import sqlite3
import time
import datetime
import random
variable = 1
bttn_clicks=0
button_dict = {}
button_dic = {}
#conn = sqlite3.connect('hoteldb.db')
#c = conn.cursor()
products = {} # Dictionary of products and prices
tally = [] # List of ordered products
def showqurec():
global button, data2
data2 = readqurec()
for index, dat in enumerate(data2):
button = ttk.Button(master, text=dat, command=lambda dat=dat: update_count(dat))
button.grid(row=index+1, column=0, padx=0, pady=0)
button_dict[dat] = button
def showqado():
global button, data2
data2 = readqado()
for index, dat in enumerate(data2):
button = ttk.Button(master, text=dat, command=lambda dat=dat: update_count(dat))
button.grid(row=index+1, column=1, pady=0, padx=0)
button_dict[dat] = button
def showcasho():
global button, data2
data2 = readcasho()
for index, dat in enumerate(data2):
button = ttk.Button(master, text=dat, command=lambda dat=dat: update_count(dat))
button.grid(row=index+1, column=2,padx=0, pady=0)
button_dict[dat] = button
def readfromdatabase(): # This function is never called...
return None
def readqurec(): # Get Meal from database
meal = {'Soup': 2.40, 'Fish': 3.10, 'Beef': 3.55, 'Pizza': 2.70}
products.update(meal)
return meal
def readqado(): # Get Tea from database
tea = {'Tea': 1.20, 'Coffee': 1.00, 'Soda': 1.65}
products.update(tea)
return tea
def readcasho(): # Get Fast food from database
fast_food = {'Hamburger': 1.80,'Hot dog': 1.45,'Pasta': 1.65}
products.update(fast_food)
return fast_food
def update_count(x):
global bttn_clicks,my_text,price
purchase = [x,products[x]] # Get the product and price
tally.append(purchase) # Add this purchase to the tally
print(purchase)
# Leaving the rest of the function as is ...
my_text=StringVar()
for name in data2:
my_text = button_dict[x].cget('text')
bttn_clicks += 1
def Receipt():
txtReceipt.delete("1.0", "4.0")
x = random.randint(10908, 500876)
randomRef = str(x)
Receipt_Ref.set("BILL" + randomRef)
txtReceipt.insert("1.0", 'Receipt Ref: \t\t\t'+Receipt_Ref.get()+"\t\t"+DateofOrder.get()+"\n")
txtReceipt.insert("2.0", 'Items\t\t'+'Quantity\t\t\t'+"Price \n\n")
if variable !=0:
txtReceipt.insert(END, str(my_text)+'\t\t'+str(bttn_clicks)+'\t\t\t'+""+str(bttn_clicks*3)+"\n")
master=Tk()
master.geometry('630x350+100+200')
master.title('Records')
Label = Button(master, text="meal", width=10,command=showqurec)
Label.grid(row=0, column=0)
BMILabel = Button(master, text="tea", width=10,command=showqado)
BMILabel.grid(row=0, column=1)
stateLabel = Button(master, text="fast food", width=10,command=showcasho)
stateLabel.grid(row=0, column=2)
lblReceipt = Button(master,text="Get Receipt:",anchor='w',command=Receipt)
lblReceipt.grid(row=0,column=4,sticky=W)
txtReceipt = Text(master,bg="white",width=47,height=17.5)
txtReceipt.grid(row=1,column=4,rowspan=4,columnspan=40)
Receipt_Ref=StringVar()
DateofOrder = StringVar()
master.mainloop()
推荐阅读
- javascript - 如何使用 onclick 功能更改课程前后的背景颜色
- ios - 添加应用程序组而不在 Xcode 上添加开发者帐户?
- google-cloud-platform - Stackdriver 提醒不会自动关闭
- reactjs - 序列化程序无法从 React 正确读取 POST 数据到 Django Rest 框架
- python - 如何改善分而治之的运行时间?
- html - HTML Select 不能声明 Selected 选项
- javascript - 如何为活动按钮之前的所有按钮设置相同的颜色,并为活动按钮旁边的所有按钮设置相同的颜色?
- java - 方法声明中的修饰符顺序和返回类型
- javascript - 为什么用 .after( 设置的文本不呈现为 html?
- php - 在 ImageMagick v7 上使用 clipPath 后创建透明图像