0

I'm making a point of sale system and trying to implement a button, that when pressed a new button appears but also, a window which asks the user to input an Item

def newButton ():
    w = Toplevel()
    w.title("New Item") #creates new window when button is pressed
    w.geometry("200x200")
    
    itemNameLabel = Label(w, font=("arial", 15), text="What is the item called?")
    itemNameLabel.grid(row=0, padx=5)
    itemName = Entry(w, width=18,  borderwidth=5)
    itemName.grid(row=1, padx=5)
    newItemName = itemName.get

    itemPriceLabel = Label(w, font=("arial", 15), text="What is the item's price?")
    itemPriceLabel.grid(row=4, padx=5)
    itemPrice = Entry(w, width=18,  borderwidth=5)
    itemPrice.grid(row=5, padx=5)

    def item6_Button():
        global item6_qty
        item6_price = itemPrice.get
        item6_text = newItemName
        item6_qty += 1
        item6_text = (item6_text + "    "+str(item6_price) +"    "+ str(item6_qty)) #concatonates text & variable
        item6.config(text=item6_text) #updates label text - doesn't add multiple 
        item6.pack()

        item6_Button = Button(itemFrame, text=newItemName, width=10, height=5, command=item6_Button)
        item6_Button.grid(row=7, column=1, padx=5)
        item6 = Label(receiptFrame)
    w.mainloop()


newButton= Button(itemFrame, text="Add New Button", width=20, height=5, command=newButton) #creates button for new window
newButton.place(x=480, y=600)
newButton = Label(itemFrame)

*item6_qty and item6_price are declared near the beginning of the program

This is what I have so far and although the window appears, I don't think the variables are actually set, on top of the new button appearing in the item frame. I'm not entirely sure how to go about this - do I need to use .insert for the variables?

This is the standard code I have which creates the normal button

#Item1 Button + Function
def item1_Button():
    global item1_qty #making qty variable global so it can used
    item1_text = ("Chips")
    item1_qty += 1 #increments qty variable by one everytime button is clicked
    item1_text = (item1_text + "    "+str(item1_price) +"    "+ str(item1_qty)) #concatonates text & variable
    item1.config(text=item1_text) #updates label text - doesn't add multiple 
    item1.pack() #places label within the frame
    
    
item1_Button = Button(itemFrame, text="Chips", width=10, height=5, command=item1_Button)
#creates button + links to function
item1_Button.grid(row=4, column=1, padx=5) #positions button
item1 = Label(receiptFrame)#creates label for button

Desired product

I'm not sure if I've provided enough code of what I've done to give a better picture of what I'm trying to achieve but I know large chunks of code aren't very favoured

1 Answer 1

1

here is an example of what You could do (does this help?):

from tkinter import Tk, Button, Entry, Toplevel


class MainWindow(Tk):
    def __init__(self):
        Tk.__init__(self)

        self.geometry('100x150')

        self.btn = Button(self, text='Create New!', command=self.ask)
        self.btn.pack()

    def ask(self):
        ask_window = InputWindow(self)
        ask_window.focus_force()
    
    def create(self, text):
        button = Button(self, text=text)
        button.pack()
    

class InputWindow(Toplevel):
    def __init__(self, parent):
        Toplevel.__init__(self, parent)
        self.parent = parent
        self.bind('<FocusOut>', self.destroy_)

        self.user_input = Entry(self)
        self.user_input.pack()
        
        self.submit_btn = Button(self, text='Submit!', command=self.retrieve)
        self.submit_btn.pack()
        
    def retrieve(self):
        text = self.user_input.get()
        self.parent.create(text)
        self.destroy()

    def destroy_(self, event):
        if isinstance(event.widget, Toplevel):
            self.destroy()
        

root = MainWindow()
root.mainloop()
Sign up to request clarification or add additional context in comments.

6 Comments

hello! thank you so much for this!! it works perfectly!! would you be able to explain what the functions InputWindow, retrieve and destroy do? I'm not very familiar with classes so some things aren't entirely making sense aha ^^
@STRhythm The InputWindow is a class but for understanding reasons You can just assume it is a basket that contains related functions and variables. it has a method retrieve which allows to get information from the "baskets's" varibales specifically the one that is an Entry, so basically it is just getting information from the Entry box. the destroy part as shown is not necessary I just wanted it to be able to close if focus of that windows shifts to somewhere else, so basically that You don't get huge amounts of useless windows. You might as well just destroy the window when pressing X
I hope I was clear however if You still did not understand feel free to ask, also here is a tutorial about classes so I suggest You watch it.
@STRhythm if You were asking about this part: ask_window = InputWindow(self) it just initiates the class InputWindow, it is not a function, it is a class, it is similar to say root = Tk() or button = Button(master)
also here are some tkinter sources I usually use to get information about widgets: this
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.