The following class code is an extract from a tkinter window to create a simple game ‘roc, scissors, paper’. There is one peculiar error that I can only resolve only by ‘implementing’ this error.
class RSP(Tk):
def __init__(self):
super().__init__()
self.initializeUI()
def initializeUI(self):
self.title("Simple Games")
self.geometry("400x200+0+0")
self.setupWindow()
self.attributes('-topmost', True)
self.choices = {0:'roc', 1:'scissors', 2:'paper'}
def choice(self):
self.pc_input.set(self.choices[rand.randint(0, 2)])
def setupWindow(self):
title = Label(self, text='Play Roc, Scissors & Paper', pady=20, justify='center', font=('Calibri', 12))
title.pack()
self.pc_input = StringVar()
self.pc_input.set('')
self.ddVal = StringVar()
self.ddVal.set('')
Label(self, text='', textvariable=self.pc_input, justify='left').place(x=115, y=100)
OptionMenu(self, self.ddVal, *['roc', 'paper', 'scissors'], command=self.choice).place(x=115, y=70)
app = RSP()
app.mainloop()
The error is: “TypeError: RSP.choice() takes 1 positional argument but 2 were given”.
As you can see from the code of choice, there is no argument except self.
I can resolve this by actually adding one (redundant) argument to the function:
def choice(self, x):
self.pc_input.set(self.choices[rand.randint(0, 2)])
The argument x is not used at all but Python accepts it and the class runs ok.
How can this be? Your help is much appreciated.