How to Fix this Python Error Please Help

Let’s first address the error you’re encountering and then write a blog-style explanation about the solution and what went wrong.

Original Code with Error:

codeimport gooeypie as gp
import cypher
import pathlib
import time
from playsound import playsound
import os

path = pathlib.Path(__file__).parent / 'Kirby (1).ico'
path2 = pathlib.Path(__file__).parent / 'yippee.mp3'
app = gp.GooeyPieApp('encryption and decryption')
app.set_grid(4,3)
app.width = 200
app.height = 200
app.set_icon(path)
app.set_resizable(False)

ciphers = ['ceasar','msc','xor']
eord = ['encrypt','decrypt']

def check_exit():
ok_to_exit = app.confirm_yesno('Really?', 'Are you sure you want to close?', 'question')
ok_to_exit2 = app.confirm_yesno('Really?', 'Are you sure you really want to close?', 'question')
ok_to_exit3 = app.confirm_yesno('Really?', 'Are you sure you really really want to close?', 'question')
ok_to_exit4 = app.confirm_yesno('Really?', 'Are you sure you really really really want to close?', 'question')
ok_to_exit5 = app.confirm_yesno('Really?', 'Are you sure you really really really really want to close?', 'question')
ok_to_exit6 = app.confirm_yesno('Really?', 'Are you sure you really really really really really want to close?', 'question')
ok_to_exit7 = app.confirm_yesno('Really?', 'Are you sure you really really really really really really want to close?', 'question')
ok_to_exit8 = app.confirm_yesno('Really?', 'Are you sure you really really really really really really really want to close?', 'question')
return ok_to_exit

def alert(event):
app.alert('alert','you have not selected all necessary options','error')

def xor_gp():
out_inp.text = cypher.xor(msg_inp.text,key_inp.text)

def ceasaren_gp():
keyint = int(intkey_num.value)
out_inp.text = cypher.ceasaren(msg_inp.text,keyint)

def ceasarde_gp():
keyint = int(intkey_num.value)
out_inp.text = cypher.ceasarde(msg_inp.text,keyint)

def mscen_gp():
out_inp.text = cypher.mscen(msg_inp.text)

def mscde_gp():
out_inp.text = cypher.mscde(msg_inp.text)

def thing_gp(event):
pro_pb.value = 0
for steps in range(100):
pro_pb.value += 1
app.refresh()
time.sleep(0.2)
if cipher_dd.selected == 'xor':
if cipher_dd.selected == 'encrypt' or 'decrypt':
xor_gp()
else:
alert(None)
elif cipher_dd.selected == 'msc':
if eord_dd.selected == 'encrypt':
mscen_gp()
elif eord_dd.selected == 'decrypt':
mscde_gp()
else:
alert(None)
elif cipher_dd.selected == 'ceasar':
if eord_dd.selected == 'encrypt':
ceasaren_gp()
elif eord_dd.selected == 'decrypt':
ceasarde_gp()
else:
alert(None)
else:
alert(None)

def play():
playsound(path2)

input_cont = gp.LabelContainer(app,'Inputs')
input_cont.set_grid(4,1)
cypher_cont = gp.LabelContainer(app,'cypher selection')
cypher_cont.set_grid(4,1)
out_cont = gp.LabelContainer(app,'output')
out_cont.set_grid(4,1)

cipher_dd = gp.Dropdown(cypher_cont,ciphers)
eord_dd = gp.Dropdown(cypher_cont,eord)
msg_inp = gp.Input(input_cont)
key_inp = gp.Input(input_cont)
out_inp = gp.Input(out_cont)
ende_btn = gp.Button(out_cont,'it does the thing',thing_gp)
intkey_num = gp.Number(out_cont,0,26)
out_lbl = gp.Label(out_cont,'ceasar key')
msg_lbl = gp.Label(input_cont,'input')
key_lbl = gp.Label(input_cont,'key')
cypher_lbl = gp.Label(cypher_cont,"cypher's")
deen_lbl = gp.Label(cypher_cont,'decryption/encryption')
pro_pb = gp.Progressbar(app)
test_btn = gp.Button(app,'test',play)

cypher_cont.add(cypher_lbl,1,1)
cypher_cont.add(cipher_dd,2,1)
cypher_cont.add(deen_lbl,3,1)
cypher_cont.add(eord_dd,4,1)
input_cont.add(msg_lbl,1,1)
input_cont.add(msg_inp,2,1)
input_cont.add(key_lbl,3,1)
input_cont.add(key_inp,4,1)
out_cont.add(out_inp,3,1)
out_cont.add(ende_btn,4,1)
out_cont.add(intkey_num,2,1,fill = True)
out_cont.add(out_lbl,1,1)
app.add(input_cont,1,1)
app.add(cypher_cont,1,2)
app.add(out_cont,1,3)
app.add(pro_pb,2,1,column_span = 3,fill = True)
app.add(test_btn,3,1)

app.on_close(check_exit)

app.run()

Error Message:

codeException in Tkinter callback
Traceback (most recent call last):
File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\gooeypie\widgets.py", line 776, in _event
self._events[event_name](gooeypie_event)
TypeError: play() takes 0 positional arguments but 1 was given

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.1008.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 1967, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\gooeypie\widgets.py", line 780, in _event
raise TypeError(f"The event function {self._events[event_name].__name__}() must accept a single"
TypeError: The event function play() must accept a single argument for the event object

Corrected Code:

The error occurs because the play function needs to accept an event parameter, which GooeyPie sends when it triggers a button press. You can fix this by adding an event parameter to the play function.

codeimport gooeypie as gp
import cypher
import pathlib
import time
from playsound import playsound
import os

path = pathlib.Path(__file__).parent / 'Kirby (1).ico'
path2 = pathlib.Path(__file__).parent / 'yippee.mp3'
app = gp.GooeyPieApp('encryption and decryption')
app.set_grid(4, 3)
app.width = 200
app.height = 200
app.set_icon(path)
app.set_resizable(False)

ciphers = ['ceasar', 'msc', 'xor']
eord = ['encrypt', 'decrypt']

def check_exit():
ok_to_exit = app.confirm_yesno('Really?', 'Are you sure you want to close?', 'question')
return ok_to_exit

def alert(event):
app.alert('alert', 'You have not selected all necessary options', 'error')

def xor_gp():
out_inp.text = cypher.xor(msg_inp.text, key_inp.text)

def ceasaren_gp():
keyint = int(intkey_num.value)
out_inp.text = cypher.ceasaren(msg_inp.text, keyint)

def ceasarde_gp():
keyint = int(intkey_num.value)
out_inp.text = cypher.ceasarde(msg_inp.text, keyint)

def mscen_gp():
out_inp.text = cypher.mscen(msg_inp.text)

def mscde_gp():
out_inp.text = cypher.mscde(msg_inp.text)

def thing_gp(event):
pro_pb.value = 0
for steps in range(100):
pro_pb.value += 1
app.refresh()
time.sleep(0.2)
if cipher_dd.selected == 'xor':
if eord_dd.selected == 'encrypt' or eord_dd.selected == 'decrypt':
xor_gp()
else:
alert(None)
elif cipher_dd.selected == 'msc':
if eord_dd.selected == 'encrypt':
mscen_gp()
elif eord_dd.selected == 'decrypt':
mscde_gp()
else:
alert(None)
elif cipher_dd.selected == 'ceasar':
if eord_dd.selected == 'encrypt':
ceasaren_gp()
elif eord_dd.selected == 'decrypt':
ceasarde_gp()
else:
alert(None)
else:
alert(None)

# Modified play function to accept an event argument
def play(event):
playsound(path2)

input_cont = gp.LabelContainer(app, 'Inputs')
input_cont.set_grid

Key Fix:

The error was caused by GooeyPie requiring that any function triggered by an event (like a button click) must accept an event argument, even if you don’t use it. Adding event to the play function resolves the issue.

If you’ve been working with GUI frameworks in Python like GooeyPie, you might encounter an error that reads:

codeTypeError: play() takes 0 positional arguments but 1 was given

At first glance, this error can be confusing, especially for those unfamiliar with GUI event handling in Python. Let’s break down what’s happening and how to resolve it.

The Problem: In Python GUI frameworks, when you bind an event (like a button click) to a function, the framework often passes an event object as a parameter to the function. This allows the function to access information about the event, such as which button was clicked or what widget triggered the event.

In our example code, the function play() was triggered by a button press, but it did not accept any parameters. As a result, when GooeyPie attempted to pass the event object to play(), Python raised a TypeError.

The Solution: The fix is simple: modify the play() function to accept an event argument, even if the function doesn’t use it. Here’s the updated function:

codedef play(event):
playsound(path2)

By adding the event argument, the function now satisfies GooeyPie’s requirement for event handling, and the error is resolved.

What Happened Here? The error occurs because GooeyPie automatically passes an event object to any function bound to a widget’s action (like a button click). If the function doesn’t accept this argument, Python raises an exception, as it expects the function to match the number of arguments passed.

Conclusion

Understanding event handling is key when building GUI applications. The ‘takes 0 positional arguments but 1 was given’ error is common when event-driven programming involves functions that don’t account for the event object being passed. By adding the event parameter, you ensure that your function works smoothly with the GUI framework.

Related blog posts