适用于单个产品和尺寸的Python Tkinter按钮

时间:2019-07-10 11:00:01

标签: python oop tkinter

我是Python和Tkinter的新手。我正在尝试创建一个程序,让用户按下饮料,饮料的大小和口味。当他们按下添加按钮时,我希望将此信息写入数据库表中

我不知道该怎么做。有人可以帮忙吗?

我尝试为每个单独的命令创建一个函数,该函数在按下每个按钮时运行,但是我不得不创建一个大型的else-if结构来映射每种可能的饮料和尺寸组合。有更有效的方法吗?

global small
global medium
global large
global Espresso
global Cappucino

small = False
medium = False
large = False
Espresso = False
Cappucino = False

def Espresso():
    global Espresso
    Espresso = True
def Cappucino():
    global Cappucino
    Cappucino = True
def sizeSmall():
    global small
    small = True
def sizeMedium():
    global medium
    medium = True
def sizeLarge():
    global large
    large = True

def add():
    global Espresso
    global Cappucino
    global small
    global medium
    global large

if Espresso == True and small == True:
        backend.insert("Espresso","small")
        Espresso = False
        small = False
    elif Espresso == True and medium == True:
        backend.insert("Espresso","medium")
        Espresso = False
        medium = False
    elif Espresso == True and large == True:
        backend.insert("Espresso","large")
        Espresso = False
        large = False
    elif Cappucino == True and small == True:
        backend.insert("Cappucino","small")
        Cappucino = False
        small = False
    elif Cappucino == True and medium == True:
        backend.insert("Cappucino","medium")
        Espresso = False
        small = False
    elif Cappucino == True and large == True:
        backend.insert("Cappucino","large")
        Cappucino = False

#Small button
smallImage = PhotoImage(file="ResizedItemImages/Function/smallResized.png")
smallButton = Button(topFrame,image=smallImage,command=sizeSmall)
smallButton.grid(column=0,row=4,pady=(15,0))

#Medium Button
mediumImage = PhotoImage(file="ResizedItemImages/Function/medium_Resized.png")
mediumButton = Button(topFrame,image=mediumImage,command=sizeMedium)
mediumButton.grid(column=1,row=4,pady=(15,0))

#Large button
largeImage = PhotoImage(file="ResizedItemImages/Function/largeResized.png")
largeButton = Button(topFrame,image=largeImage,command=sizeLarge)
largeButton.grid(column=2,row=4,pady=(15,0))

#Add button
addImage = PhotoImage(file="ResizedItemImages/Function/addResized.png")
addButton = Button(topFrame,image=addImage,command=add)
addButton.grid(column=3,row=4,pady=(15,0),sticky=E)

#Cappucino Button
cappucinoImage = PhotoImage(file="ResizedItemImages/HotDrinks/cappucinoResized.png")
cappucinoButton = Button(topFrame,image=cappucinoImage,command=Cappucino)

#Espresso Button
espressoImage = PhotoImage(file="ResizedItemImages/HotDrinks/espressoResized.png")
espressoButton = Button(topFrame,image=espressoImage,command=Espresso)

#BACKEND MODULE
def insert(product,size):
    conn=sqlite3.connect("foods.db")
    cur=conn.cursor()
    cur.execute("INSERT INTO food VALUES (NULL,?,?)",(product,size))
    conn.commit()
    conn.close()

window.title("Coffee System")
window.geometry("830x800")
window.resizable(width=False, height=False)
window.mainloop()

1 个答案:

答案 0 :(得分:0)

  

我尝试为每个单独的命令创建一个函数,该函数在按下每个按钮时运行,但是我不得不创建一个大型的else-if结构来映射每种可能的饮料和尺寸组合。有更有效的方法吗?

是的,有。风味和尺寸按钮需要设置一个值,而不是调用一个函数。然后,您的添加按钮可以获取值并将其保存到数据库中。

由于口味和尺寸是唯一的选择(即:您只能选择一种尺寸和一种口味),因此应使用单选按钮。每个组可以共享一个变量。

例如,从定义所有可能的口味和大小开始。这些应该是您要保存到数据库的值。我们将使其保持简单,并使数据库值和用户在GUI中看到的值相同。

FLAVORS = ("Cappucino", "Espresso")
SIZES = ("Small", "Medium", "Large")

我们还需要创建几个变量来保存当前值。由于我们希望tkinter能够获取并设置值,因此我们将使用StringVar

flavorVar = tk.StringVar(value=FLAVORS[0])
sizeVar = tk.StringVar(value=SIZES[0])

我们可以循环创建单选按钮,而不是一次创建一个。此代码假定您已创建名为flavorFramesizeFrame的框架来容纳单选按钮。

for flavor in FLAVORS:
    rb = tk.Radiobutton(flavorFrame, text=flavor, value=flavor, variable=flavorVar)
    rb.pack(side="top", anchor="w")

for size in SIZES:
    rb = tk.Radiobutton(sizeFrame, text=size, value=size, variable=sizeVar)
    rb.pack(side="top", anchor="w")

最后,我们需要一个添加按钮,以及获取值并将其放入数据库的代码。在本例中,我们将仅打印值而不是数据库。这段代码假定您已经创建了一个名为buttonFrame的框架。

def add():
    size = sizeVar.get()
    flavor = flavorVar.get()

    print("flavor: {}, size: {}".format(flavor, size))

addButton = tk.Button(buttonFrame, text="Add", command=add)

要将值保存到数据库,只需将print语句替换为所需的任何代码即可。

将它们放在一起,这是最终版本:

import tkinter as tk

FLAVORS = ("Cappucino", "Espresso")
SIZES = ("Small", "Medium", "Large")

def add():
    size = sizeVar.get()
    flavor = flavorVar.get()

    print("flavor: {}, size: {}".format(flavor, size))

root = tk.Tk()

flavorVar = tk.StringVar(value=FLAVORS[0])
sizeVar = tk.StringVar(value=SIZES[0])

flavorFrame = tk.LabelFrame(root, text="Flavor")
sizeFrame = tk.LabelFrame(root, text="Size")
buttonFrame = tk.Frame(root)

buttonFrame.pack(side="top")
flavorFrame.pack(side="left", fill="both", expand=True, padx=20, pady=20)
sizeFrame.pack(side="right", fill="both", expand=True, padx=20, pady=20)

for flavor in FLAVORS:
    rb = tk.Radiobutton(flavorFrame, text=flavor, value=flavor, variable=flavorVar)
    rb.pack(side="top", anchor="w")

for size in SIZES:
    rb = tk.Radiobutton(sizeFrame, text=size, value=size, variable=sizeVar)
    rb.pack(side="top", anchor="w")

addButton = tk.Button(buttonFrame, text="Add", command=add)
addButton.pack(side="left")

tk.mainloop()