Functions running before they should be - Python Tkinter logic error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Functions running before they should be - Python Tkinter logic error (/thread-22690.html) |
Functions running before they should be - Python Tkinter logic error - jhf2 - Nov-22-2019 I am creating a tkinter program. There is always one button - 'Place' - on the screen. When that button is clicked I want two more buttons to appear and the user is only supposed to click one of them. For some reason this is not working and I am getting a logic error. Right now when the user clicks on 'Place', the commands of the other two buttons are automatically accessed. I don't want the commands on these buttons accessed until the user clicks on them. Here is my code: python & tkinter from tkinter import * from tkinter import scrolledtext # Window window = Tk() window.geometry("750x500") window.configure(background='gray') window.title("Fantasy Betting Log") # Bank bank = 100 bankLbl = Label(window, text="Bank: " + str(bank)) bankLbl.place(x=0, y=0) # Functions def win(wager, odds): if int(odds) > 0: return float(wager) * float(odds) / 100 if int(odds) < 0: return float(wager) * 100 / abs(float(odds)) def winClicked(name, wager, odds): log.insert(INSERT,'WIN - ' + name + ' won you ' + str(win(wager, odds))) log.insert(END, "$ \n") def loseClicked(name, wager): log.insert(INSERT,'LOSS - ' + name + ' lost you ' + str(wager)) log.insert(END, "$ \n") counter = 0 def newLiveBet(name, wager, odds): global counter liveBet = Label(window, text= name + " - Wager: " + wager + ", Odds: " + odds) liveBet.place(x=10, y=(300 + (30 * counter))) winButton = Button(window, text="Win", width=3, bg="white", fg="green", command=winClicked(name,wager, odds)) winButton.place(x= 400, y = (300 + (30 * counter))) loseButton = Button(window, text="Lose", width=3, bg="white", fg="red", command=loseClicked(name, wager)) loseButton.place(x= 450, y = (300 + (30 * counter))) counter += 1 def placeClicked(): name = betName.get() wager = wagerAmt.get() odds = oddsAmt.get() betName.delete(0, END) wagerAmt.delete(0, END) oddsAmt.delete(0, END) newLiveBet(name, wager, odds) #Theres more code here but irrelevant to question window.mainloop()So essentially when the button 'place' is clicked, 'winClicked()' and 'loseClicked' also act like they were clicked when all I want is for them to appear on the screen. RE: Functions running before they should be - Python Tkinter logic error - Larz60+ - Nov-23-2019 I see where you define the new buttons, but not the first one. Also, you want to include code in NewLiveBet to make sure you don't try to recreate two already created Buttons. or delete the existing ones before recreating. RE: Functions running before they should be - Python Tkinter logic error - jhf2 - Nov-23-2019 (Nov-23-2019, 01:49 AM)Larz60+ Wrote: I see where you define the new buttons, but not the first one. Also, you want to include code in NewLiveBet to make sure you don't try to recreate two already created Buttons. or delete the existing ones before recreating. I found a way to fix it. I added "lambda" before calling the new functions in the button commands. So the button's code looks like this now: button = Button(window, text='I am a button', command=lambda:buttonClickedFunction) |