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
  [Tkinter] tkinter issue with variables carrying over between functions PengEng 1 210 Apr-06-2020, 06:13 PM
Last Post: deanhystad
  Functions with Tkinter Reldaing 2 808 Jan-03-2020, 06:57 PM
Last Post: Reldaing
  def functions in tkinter Fureneshi 5 463 Dec-26-2019, 11:34 PM
Last Post: woooee
  tkinter GUI, problem running seperate files fishglue 17 1,100 Oct-15-2019, 02:56 PM
Last Post: Denni
  Binding functions to Menus in tkinter?? Mocap 1 474 Jul-23-2019, 01:37 AM
Last Post: Larz60+
  [Tkinter] Error in Tkinter moste 6 1,382 Apr-11-2019, 09:37 AM
Last Post: moste
  Variable not sharing same value between two different functions Python 2.7 Tkinter albert 0 750 Aug-31-2018, 10:45 AM
Last Post: albert

Forum Jump:


Users browsing this thread: 1 Guest(s)