0

I am trying to make a program that records user input (drinks) and displays these inputs when a person wants to view them. .

Specifically, I want the drinks to be displayed when the user clicks the 'View Log' button (second_button). I think this needs to be done in the viewlog function, but I am stuck with the best way to do this. I am unsure of how to best store the inputs and how to retrieve them.

from tkinter import *
root = Tk()
root.title('Simple App')

first_label = Label(root, text="Welcome to the program", font=('Arial', 20)).pack()

def save_drink(added_drink, top):
    global drinks_list
    drinks_list = []
    newtop = Toplevel(root)
    newtop.geometry("200x200")
    newtop.title("Drink Added")
    label = Label(
        newtop,
        text= "{} Added".format((added_drink.get())), font=('Mistral 20')).pack()
    close_btn = Button(newtop, text='Close', font=('Mistral 20'), command=lambda t=top, n=newtop: close(t,n))
    close_btn.pack()
    drinks_list.append(added_drink.get())

def add_drink():
    top = Toplevel(root)
    top.geometry("750x250")
    top.title("Record Drink")
    label = Label(top, text= "What drink did you have?", font=('Mistral 18')).pack()
    added_drink = Entry(top, font=6)
    added_drink_string = str(added_drink)
    added_drink.pack()
    added_drink_button = Button(top, text='Add Drink', font=3,
                                command=lambda: save_drink(added_drink, top)).pack()

def close(top, newtop):
    top.destroy()
    newtop.destroy()

def viewlog():
    logtop = Toplevel(root)
    logtop.title('Drinks Log')
    label_text = Label(logtop, text="Today's Drinks", font=('Arial', 20)).pack()
    label2 = Label(logtop, text=print(drinks_list), font=('Arial', 16))

    # here we need to take the items in drinks_list and display them on a screen
    close_btn = Button(logtop, text="Close", pady=20, font=('Times New Roman', 20), command=logtop.destroy).pack()


first_button = Button(root, text="Add drink", command=add_drink).pack()
second_button = Button(root, text="View log", command=viewlog).pack()

root.mainloop()
pc510895
  • 69
  • 1
  • 7

1 Answers1

0

you need to pack the label, and remove print() from the label text variable

label2 = Label(logtop, text=(drinks_list), font=('Arial', 16)).pack()
we_stl
  • 45
  • 7
  • That is only part of the problem. Also please correct, your code before we get another duplicate of [this problem](https://stackoverflow.com/q/1101750/11106801). – TheLizzard Oct 01 '22 at 19:12
  • The solution proposed by we_stl works, but it only returns the most recent added drink. If I want to add multiple drinks, I am unable to do so. – pc510895 Oct 02 '22 at 01:20
  • @pc510895 It is because you reset `drinks_list` to empty list inside `save_drink()`. – acw1668 Oct 02 '22 at 01:35
  • Then what's the most efficient way to create some variable called `drink list` that I can retrieve later when the `second_button` is clicked? – pc510895 Oct 02 '22 at 03:22
  • 1
    @pc510895 Just move `drinks_list = []` to outside `save_drinks()`. – acw1668 Oct 02 '22 at 15:20