from tkinter import *
class GameBoard(Frame):
def __init__(self):
top = Toplevel()
Frame.__init__(self)
self.master.title("test")
self.grid()
#button frame
self.__buttonPane = Frame(self)
self.__buttonPane.grid()
#buttons
self.__buttonA1 = Button(self.__buttonPane,text = "A1",command = self._test("A"))
self.__buttonA1.grid()
def _test(self,test):
if self.__buttonA1["state"] == NORMAL:
print(test)
def main():
GameBoard().mainloop()
main()
This code will not work due to the variable and if I remove the variable test and make it
from tkinter import *
class GameBoard(Frame):
def __init__(self):
top = Toplevel()
Frame.__init__(self)
self.master.title("test")
self.grid()
#button frame
self.__buttonPane = Frame(self)
self.__buttonPane.grid()
#buttons
self.__buttonA1 = Button(self.__buttonPane,text = "A1",command = self._test)
self.__buttonA1.grid()
def _test(self):
if self.__buttonA1["state"] == NORMAL:
print("hi")
def main():
GameBoard().mainloop()
main()
How would I fix my code so it would allow for input of a variable?
This is just a test I am doing so I can make it work in a much bigger code.
When you specify
command = self._test,self._testis a function that will be called.self._test("A")is not a function, it’s a call to a function (that returnsNonebecause there’s noreturnstatement in the definition).You can write ‘a function that calls
self._test("A")as eitherlambda: self._test("A"), orfunctools.partial(self._test, "A"), or explicitly inGameBoard.__init__: