首页 > 解决方案 > 用于单个产品和尺寸的 Python 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()

标签: pythonooptkinter

解决方案


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

就在这里。风味和大小按钮需要设置一个值而不是调用一个函数。然后,您的添加按钮可以获取值并将它们保存到数据库中。

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

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

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

我们还需要创建几个变量来保存当前值。由于我们希望 tkinter 能够获取和设置值,我们将使用 a 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()

推荐阅读