Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Functions running before they should be - Python Tkinter logic error
#1
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.
Quote
#2
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.
Quote
#3
(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)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  def functions in tkinter Fureneshi 3 75 Dec-13-2019, 10:45 PM
Last Post: michael1789
  tkinter GUI, problem running seperate files fishglue 17 561 Oct-15-2019, 02:56 PM
Last Post: Denni
  Binding functions to Menus in tkinter?? Mocap 1 223 Jul-23-2019, 01:37 AM
Last Post: Larz60+
  [Tkinter] Error in Tkinter moste 6 683 Apr-11-2019, 09:37 AM
Last Post: moste
  tkinter label error nonzzo 1 692 Mar-31-2019, 01:29 PM
Last Post: Larz60+
  Tkinter error for a scheduled event Ceegen 5 1,280 Jan-14-2019, 09:24 PM
Last Post: Ceegen
  Tkinter Gui Menu Addition Error whisperquiet 2 524 Jan-04-2019, 09:29 PM
Last Post: whisperquiet
  Variable not sharing same value between two different functions Python 2.7 Tkinter albert 0 575 Aug-31-2018, 10:45 AM
Last Post: albert
  Tkinter's strange error Ponomarenko Pavlo 7 4,159 Jul-20-2017, 06:12 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)