dimanche 20 janvier 2019

Tkinter Conditional Buttons

I am trying to code GUI that allows a user to choose between either chocolate or sweets and then a size of either small, medium, or large. They then click the 'add' button to write either chocolate or sweets and the size either small, medium, or large to the database. The database contents should be displayed in a list box in the GUI.

When I press an item and a size then press add, or when i press a size and press add, or a button and press add, i get the error message:

Must choose item and quantity before adding

My front end file:

from tkinter import *
import backend

window = Tk()
window.wm_title("System")

global choc_selected
choc_selected = False
global sweet_selected
sweet_selected = False
global size_small
size_small = False
global size_medium
size_medium = False
global size_large
size_large = False


def chocolate(event):
    choc_selected = True
def sweets(event):
    sweet_selected = True

def sizeSmall(event):
    size_small = True
def sizeMedium(event):
    size_medium = True
def sizeLarge(event):
    size_large = True

def addItem(event):
    global choc_selected
    global sweet_selected
    global size_small
    global size_medium
    global size_large
    if choc_selected == True:
        if size_small == True:
            print("Choc Small")
        elif size_medium == True:
            print("Choc medium")
        elif size_large == True:
            print("Choc large")
        else:
            print("Must choose size before adding")
    elif sweet_selected == True:
        if size_small == True:
            print("Sweet small")
        elif size_medium == True:
            print("Sweet medium")
        elif size_large == True:
            print("Sweet large")
    else:
        print("Must choose size before adding")
else:
    print("Must choose item and quantity before adding")


list1=Listbox(window, height=6,width=35,exportselection=0)
list1.grid(row=2,column=0,rowspan=6,columnspan=2)

b1=Button(window,text="Add Chocolate",width=12)
b1.bind('<Button-1>', chocolate)
b1.grid(row=4,column=3)

b3=Button(window,text="Add Sweets",width=12)
b3.bind('<Button-1>', sweets)
b3.grid(row=5,column=3)

b2=Button(window,text="Add",width=12)
b2.bind('<Button-1>', addItem)
b2.grid(row=7,column=4)

b4=Button(window,text="Add Small Size",width=12)
b4.bind('<Button-1>', sizeSmall)
b4.grid(row=4,column=4)

b5=Button(window,text="Add Medium Size",width=12)
b5.bind('<Button-1>', sizeMedium)
b5.grid(row=5,column=4)

b6=Button(window,text="Add Large Size",width=12)
b6.bind('<Button-1>', sizeLarge)
b6.grid(row=6,column=4)


window.mainloop()

My backend file:

import sqlite3

def connect():
    conn=sqlite3.connect("food.db")
    cur=conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS menu(id INTEGER PRIMARY KEY, 
    product text, quantity text)")
    conn.commit()
    conn.close()

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

def view():
    conn=sqlite3.connect("books.db")
    cur=conn.cursor()
    cur.execute("SELECT * FROM menu")
    rows=cur.fetchall()
    conn.close()
    return rows

connect()

Aucun commentaire:

Enregistrer un commentaire