How can I output the button widgets first before the entry and label widgets?

50 views Asked by At

I'm working out for out final project. I think there's an error in my frames, however I can't fix it. I want to have the frame with the buttons first. Then, for example, I click the Carrots button, and it will switch frames for the entry and label widgets.

import tkinter as tk
import sqlite3

def submit_button(frame, entryId, entryName, entryPrice, entryQuantity):
conn = sqlite3.connect("eclistdata3.db")
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS inventory(itemId TEXT, itemName TEXT, itemPrice TEXT, itemQuantity TEXT)")
cursor.execute("INSERT INTO inventory VALUES (?, ?, ?, ?)", (entryId.get(), entryName.get(), entryPrice.get(), entryQuantity.get()))
conn.commit()
conn.close()
frame.pack_forget()
frame.place_forget()
frame1.pack()
frame1.place(relx=0.45, rely=0.23)
entryId.delete(0, tk.END)
entryName.delete(0, tk.END)
entryPrice.delete(0, tk.END)
entryQuantity.delete(0, tk.END)

def create_entry_frame(window, itemName, buttonText, x, y):
frame = tk.Frame(window, bg="gray")
frame.pack()
frame.place(relx=0.45, rely=0.23)

idLabel = tk.Label(frame, text="Item No.", font=('Roboto bold', 10), bg='gray')
nameLabel = tk.Label(frame, text="Item Name", font=('Roboto bold', 10), bg='gray')
priceLabel = tk.Label(frame, text="Price", font=('Roboto bold', 10), bg='gray')
quantityLabel = tk.Label(frame, text="Quantity", font=('Roboto bold', 10), bg='gray')

entryId = tk.Entry(frame, width=15, bd=5, font=('Roboto', 10, 'bold'))
entryName = tk.Entry(frame, width=15, bd=5, font=('Roboto', 10, 'bold'))
entryName.insert(0, itemName)
entryName.configure(state=tk.DISABLED)
entryPrice = tk.Entry(frame, width=15, bd=5, font=('Roboto', 10, 'bold'))
entryQuantity = tk.Entry(frame, width=15, bd=5, font=('Roboto', 10, 'bold'))

idLabel.pack()
idLabel.place()
entryId.pack()
entryId.place()
nameLabel.pack()
nameLabel.place()
entryName.pack()
entryName.place()
priceLabel.pack()
priceLabel.place()
entryPrice.pack()
entryPrice.place()
quantityLabel.pack()
quantityLabel.place()
entryQuantity.pack()
entryQuantity.place()

submit = tk.Button(frame, text="Submit", command=lambda: submit_button(frame, entryId, entryName, entryPrice, entryQuantity))
submit.pack()
submit.place()

button = tk.Button(frame1, text=buttonText, command=lambda: (frame.pack_forget(), frame.place_forget(), frame.pack(), frame.place(relx=0.45, rely=0.23)))
button.pack()
button.place()

window = tk.Tk()

frame1 = tk.Frame(window, bg="gray")
frame1.pack()
frame1.place(relx=0.45, rely=0.23)

create_entry_frame(window, "Carrots", "Carrots", 0, 1)
create_entry_frame(window, "Broccoli", "Broccoli", 0, 2)
create_entry_frame(window, "Tomatoes", "Tomatoes", 0, 3)
create_entry_frame(window, "Cucumbers", "Cucumbers", 0, 4)
create_entry_frame(window, "Squash", "Squash", 0, 5)

window.mainloop()

Moreover, this is the output that comes first:

Output.

However, I want this frame to come first:

Desired output

1

There are 1 answers

0
George On

I have improved item frame( frame ) deletion in an item creating function( create_entry_frame ). Now this function is only adding new item(not opening the item frame).
This is the correct code:

import tkinter as tk
import sqlite3

def submit_button(frame, entryId, entryName, entryPrice, entryQuantity):
    conn = sqlite3.connect("eclistdata3.db")
    cursor = conn.cursor()
    cursor.execute("CREATE TABLE IF NOT EXISTS inventory(itemId TEXT, itemName TEXT, itemPrice TEXT, itemQuantity TEXT)")
    cursor.execute("INSERT INTO inventory VALUES (?, ?, ?, ?)", (entryId.get(), entryName.get(), entryPrice.get(), entryQuantity.get()))
    conn.commit()
    conn.close()
    frame.pack_forget()
    frame.place_forget()
    frame1.pack()
    frame1.place(relx=0.45, rely=0.23)
    entryId.delete(0, tk.END)
    entryName.delete(0, tk.END)
    entryPrice.delete(0, tk.END)
    entryQuantity.delete(0, tk.END)

def create_entry_frame(window, itemName, buttonText, x, y):
    button = tk.Button(frame1, text=buttonText, command=lambda: (frame.pack_forget(), frame.place_forget(), frame.pack(), frame.place(relx=0.45, rely=0.23)))
    button.pack()
    button.place()
    
    frame = tk.Frame(window, bg="gray")
    frame.pack()
    frame.place(relx=0.45, rely=0.23)

    idLabel = tk.Label(frame, text="Item No.", font=('Roboto bold', 10), bg='gray')
    nameLabel = tk.Label(frame, text="Item Name", font=('Roboto bold', 10), bg='gray')
    priceLabel = tk.Label(frame, text="Price", font=('Roboto bold', 10), bg='gray')
    quantityLabel = tk.Label(frame, text="Quantity", font=('Roboto bold', 10), bg='gray')

    entryId = tk.Entry(frame, width=15, bd=5, font=('Roboto', 10, 'bold'))
    entryName = tk.Entry(frame, width=15, bd=5, font=('Roboto', 10, 'bold'))
    entryName.insert(0, itemName)
    entryName.configure(state=tk.DISABLED)
    entryPrice = tk.Entry(frame, width=15, bd=5, font=('Roboto', 10, 'bold'))
    entryQuantity = tk.Entry(frame, width=15, bd=5, font=('Roboto', 10, 'bold'))

    idLabel.pack()
    idLabel.place()
    entryId.pack()
    entryId.place()
    nameLabel.pack()
    nameLabel.place()
    entryName.pack()
    entryName.place()
    priceLabel.pack()
    priceLabel.place()
    entryPrice.pack()
    entryPrice.place()
    quantityLabel.pack()
    quantityLabel.place()
    entryQuantity.pack()
    entryQuantity.place()

    submit = tk.Button(frame, text="Submit", command=lambda: submit_button(frame, entryId, entryName, entryPrice, entryQuantity))
    submit.pack()
    submit.place()
    
    frame.pack_forget()
    frame.place_forget()

window = tk.Tk()

frame1 = tk.Frame(window, bg="gray")
frame1.pack()
frame1.place(relx=0.45, rely=0.23)

create_entry_frame(window, "Carrots", "Carrots", 0, 1)
create_entry_frame(window, "Broccoli", "Broccoli", 0, 2)
create_entry_frame(window, "Tomatoes", "Tomatoes", 0, 3)
create_entry_frame(window, "Cucumbers", "Cucumbers", 0, 4)
create_entry_frame(window, "Squash", "Squash", 0, 5)

window.mainloop()

It returns:

first

But when I click Broccoli it returns:

second

When I send, it returns:

third