Skip to content
Advertisement

Adding button (with variables) by pressing button – tkinter

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

Advertisement

Answer

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()
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement