Python Tkinter按钮用于单个产品和大小

2024-05-26 16:27:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python和Tkinter是新手。我试图创建一个程序,我希望用户按下饮料和饮料的大小和口味。当他们按add按钮时,我想将这些信息写入数据库表

我不知道怎么做。有人能帮忙吗?你知道吗

我曾尝试为每个按钮按下时运行的单个命令创建一个函数,但我不得不创建一个大型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()

Tags: andbackendfalsetruedefbuttonglobalfile
1条回答
网友
1楼 · 发布于 2024-05-26 16:27:03

I have tried creating a function for each individual command which runs when each button is pressed, but I have had to create a large else-if structure to map every possible drink and size combination. Is there a more efficient way to do this?

是的,有。flavor和size按钮需要设置一个值,而不是调用一个函数。然后,您的add按钮可以获取这些值并将它们保存到数据库中。你知道吗

因为口味和大小是唯一的选择(即:你只能选择一种大小,一种口味),你应该使用单选按钮。每个组可以共享一个变量。你知道吗

例如,从定义所有可能的口味和大小开始。这些应该是要保存到数据库中的值。我们将保持简单,并使数据库值和用户在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")

最后,我们需要一个add按钮,以及获取值并将其放入数据库的代码。在本例中,我们将只打印值,而不是数据库。这段代码假设您已经创建了一个名为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()

相关问题 更多 >

    热门问题