Python Forum
Functions running before they should be - Python Tkinter logic error
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.
Reply


Messages In This Thread
Functions running before they should be - Python Tkinter logic error - by jhf2 - Nov-22-2019, 11:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 1,449 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 4,919 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter Anan 1 2,598 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  Help with tkinter gui and functions Omer_ 0 1,494 Sep-22-2020, 11:43 AM
Last Post: Omer_
  [Tkinter] tkinter issue with variables carrying over between functions PengEng 1 1,703 Apr-06-2020, 06:13 PM
Last Post: deanhystad
  Functions with Tkinter Reldaing 2 2,526 Jan-03-2020, 06:57 PM
Last Post: Reldaing
  def functions in tkinter Fureneshi 5 7,470 Dec-26-2019, 11:34 PM
Last Post: woooee
  tkinter GUI, problem running seperate files fishglue 17 6,240 Oct-15-2019, 02:56 PM
Last Post: Denni
  Binding functions to Menus in tkinter?? Mocap 1 2,424 Jul-23-2019, 01:37 AM
Last Post: Larz60+
  Variable not sharing same value between two different functions Python 2.7 Tkinter albert 0 2,366 Aug-31-2018, 10:45 AM
Last Post: albert

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020