Pycharm and guizero - POS system

Hello everyone. I’m a SUPER new student. I’m trying to create an order system for my final project.
My goal is to make buttons that the user can select and it adds that item along with quantity. I’m using a loop to read a csv file for the menu and it adds each item as a button. I’m stuck on once the button is pushed, how to I get that specific item to add to the order? Can anyone help me with the next step? I asked my teacher and I didn’t really understand the answer.

Hello,

what you have to do is associate each button with a callback function. You can do this with the command keyword in when you’re configuring the button as shown in the following example.
Study the example carefully and notice the callback function setup. Note that the callback function can either be a lambda function or an explicit function.

import tkinter as tk

class App(tk.Tk):

    def __init__(self):

        super().__init__()

        self.btn1 = tk.Button(self, text="Lambda callback",
                              command=lambda: print("Hello, from lambda!"))
        self.btn1.pack(padx=70, pady=10)

        self.btn2 = tk.Button(self, text="Function callback",
                              command=self.print_greeting)
        self.btn2.pack(padx=70, pady=10)

    def print_greeting(self):
        print('Hello, from a function!')


if __name__ == "__main__":
    
    app = App()
    app.title("My Tkinter app")
    app.mainloop()

In the callback function, write the code that you want performed each time that the button is pressed.

1 Like

It’s telling me that self isn’t valid when I try to put it in my code.

That’s probably because your code is not class based. Just removed the self.

Man this is hard to process! Thank you for being patient with me. This is my code for my buttons. menu is an edittable csv file. so I can’t really do that first one, right? or am I misunderstanding? Also this is in guizero.

for i in menu:
    price = i[2]
    item = (f'{i[1]:<14}\tPrice: $ ',price)
    push = PushButton(left_box, text=item, command=askq())

Ok, I am not familiar with guizero. I thought that you were using tkinter. In that case, I am sorry, but I can’t help you (maybe someone else on this forum who is familiar with it can). You will have to do your own research with guizero. :v:

Just an fyi …

you can start here.

https://lawsie.github.io/guizero/resources/

https://projects.raspberrypi.org/en/projects/getting-started-with-guis/5

Thank you!! I appreciate you. I have been reseaching on that top link and I can’t seem to find anything that can help. I’m just going to mark it up that I cant do it the way I want to and rework the project another way. I don’t think programming will be my major after this lmao

The second link provides you with a good example. Copy it and run it in Pycharm.

In your case, have you defined the function askq() for which the command keyword assignment is referencing? If you haven’t, you need to do so.

for i in menu:
    price = i[2]
    item = (f'{i[1]:<14}\tPrice: $ ',price)
    push = PushButton(left_box, text=item, command=askq())

The first line price = i[2] seems a bit odd, as the index (the number inside the brackets) is fixed. In the following line, where you create a string, you reference i[1] when in fact you’re passing in i[2].

Now, I don’t know what menu is, but you need to verify that what you’re passing in are the correct values. To do this, you can include print statements for price, and item. Do they match the expected outcomes?

The thing about programming is that it is very methodical and logical. :wink:

So that you don’t get discouraged, start small. Find a book that teaches the fundamentals in a linear and methodical way without forcing you into projects that have components that have yet to be covered.

P.S.
There is an excellent book whose latest version will be coming out early next year:
Learning Python, 6th Edition by Mark Luntz

If you are really serious about learning Python, this book is highly recommended.
You can find it here in its early release:

Have you watched 1-2 tutorials about using guizero? Here are some from the past year. https://www.youtube.com/results?search_query=python+guizero+tutorial&sp=EgIIBQ%253D%253D

The tutorial should answer your question. And you will need a solid background in guizero if you will continue to use it.