首页 > 解决方案 > 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()

现在我宣布了价格,但是我如何从数据库中获取它们我还有另一个按钮点击在每个按钮上增加点击指定按钮的方式

标签: pythonloopstkinter

解决方案


获取按钮上的名称而不仅仅是第一个字母,您必须将名称设置为整个字符串,而不仅仅是第一个位置:

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

推荐阅读