[英]Python tkinter Help - button accessing other functions
In this code I have 3 separate buttons being made, two are only to be accessed after the first one is pushed.在这段代码中,我制作了 3 个单独的按钮,只有在按下第一个按钮后才能访问其中的两个。 For some reason when I run the code and press the 'Place' button, my 'winClicked' and 'loseClicked' functions are accessed even though they are not supposed to until the win/lose butting are pushed.
出于某种原因,当我运行代码并按下“Place”按钮时,我的“winClicked”和“loseClicked”功能会被访问,即使它们在按下赢/输对接之前不应该访问。 What do I need to fix so that 'winClicked' and 'loseClicked' only run when the buttons are pushed?
我需要解决什么问题才能使“winClicked”和“loseClicked”仅在按下按钮时运行?
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()
Because you set win
to be the value of inputted Text:因为您将
win
设置为输入文本的值:
win = input("Did you win your bet? (y/n)")
You defined win in this line你在这一行定义了胜利
win = input("Did you win your bet? (y/n)")
and when you call it当你调用它时
bank += win(wager, odds)
It use the string as previously defined win
, and it is a string (y, n), sot it is not callable as a function.它使用前面定义的字符串
win
,它是一个字符串 (y, n),所以它不能作为 function 调用。 You should use different variable name.您应该使用不同的变量名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.