Yabusa
Yabusa

Reputation: 579

Connecting button to run function to add labels

I just finished reading a Python book called Practical Programming 2nd ed. and thought I'd challenge myself to apply what I learned to make my first small program for work (convenience store) to create a list of chips I need to order using tkinter as the interface. I would input how many of each brand of chips I have in stock, and after pressing the 'Order' button, it would return the number of each brand of chips needed to order.

Here is the code I've got so far:

import tkinter as tk

# Initiate tkinter
window = tk.Tk()

# Create a dictionary of brand of chips with a value of how many chips should be in stock
chips = {'Miss Vickies Original': 4, 'Miss Vikies Salt & Vinegar': 4}

# Start counting rows
row_num = 0

for brand in chips:

    # Create label describing the brand of chips
    label = tk.Label(window, text=brand)
    label.grid(row=row_num, column=1)

    # Create an entry box to input number of chips in stock
    entry = tk.Entry(window)
    entry.grid(row=row_num, column=2)

    # Add one; To start at next row
    row_num += 1

# Return number of chips to order
def order(entry_data, stock_required):

    # Subtract how much is in stock from how much should be in stock 
    order_quantity = int(stock_required) - int(entry_data)

    # Create label to the left of each chip with how many should be ordered
    order_label = tk.Label(window, text="'Order: ', order_quantity")
    order_label.pack(side='left')

# Order button
button = tk.Button(window, text='Order', command=lambda: order(entry.get(), chips[1]))
button.pack(side='bottom')

window.mainloop()

I think I've confused myself with my own code lol. I'm having great difficulty figuring out how to make the button(placed at the bottom) trigger the calculation to occur and show the order_label to the left of each chips label.

A problem I see with my code that I'm not sure how to fix is the label would only be called once, as the function doesn't contain a loop. What's the recommended way to solve this?

I've checked: Tkinter: Link an Entry Widget to a Button to a Function.
But not a related problem, as I have the lambda: order function already in.

Here is how it should look like:

enter image description here

This is my first program, so any constructive criticism is encouraged!

Upvotes: 0

Views: 71

Answers (1)

Reblochon Masque
Reblochon Masque

Reputation: 36682

I added the functionality needed to make your code work as you intended; You will need to keep track of the current level of stock to make this something useful though.

import tkinter as tk


def generate_order_quantities():

    for brand, datafields in chips_entry_fields.items():
        entry, quantity_to_order, label = datafields
        recommended_stock_level = chips_stock_levels[brand]
        try:
            quantity_sold = int(entry.get())
        except ValueError:
            quantity_sold = 0
        quantity_to_order.set(min(quantity_sold, recommended_stock_level))


if __name__ == '__main__':

    # Initiate tkinter
    window = tk.Tk()

    # Create a dictionary of brand of chips_stock_levels with a value of how many chips_stock_levels should be in stock
    chips_stock_levels = {'Miss Vickies Original': 4, 'Miss Vikies Salt & Vinegar': 4}
    chips_quantities_sold = {'Miss Vickies Original': 0, 'Miss Vikies Salt & Vinegar': 0}
    chips_entry_fields = {}

    # Start counting rows
    row_num = 0

    for row, brand in enumerate(chips_stock_levels):
        # Create label describing the brand of chips_stock_levels
        label = tk.Label(window, text=brand)
        label.grid(row=row, column=1)

        # Create an entry box to input number of chips_stock_levels in stock
        entry = tk.Entry(window)
        chips_entry_fields[brand] = [entry]
        entry.grid(row=row, column=2)

        # Create label to the left of each chip with how many should be ordered
        quantity_to_order = tk.IntVar()
        chips_entry_fields[brand].append(quantity_to_order)
        # quantity_to_order.set()
        order_label = tk.Label(window, textvariable=quantity_to_order)
        chips_entry_fields[brand].append(order_label)
        order_label.grid(row=row, column=3)

    # Order button
    button = tk.Button(window, text='Order', command=generate_order_quantities)
    button.grid(row=3, column=3)

    window.mainloop()

Upvotes: 1

Related Questions