python bind not working - python

I have the basic screensaver program. I want to bind Mouse button 1 to delete everything on screen, but when I press the Button-1 I get this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\idlelib\run.py", line 121, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File "C:\Python34\lib\queue.py", line 175, in get
raise Empty
queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
TypeError: delete() takes 0 positional arguments but 1 was given
.
.
.
.
This is my program:
import tkinter
canvas=tkinter.Canvas(width=1000,height=800)
canvas.pack()
from random import *
entry1=tkinter.Entry()
entry1.pack()
def setric():
x=randrange(900)
y=randrange(700)
global a
canvas.delete('all')
farba=choice(("red","purple","blue","lightblue","green","lightgreen"))
if a>-90:
a=a-10
else:
a=-10
if entry1.get()=="":
canvas.update()
canvas.after(3000,setric)
a=0
else:
canvas.create_text(x,y,text=entry1.get(),font="Arial 40",fill=farba,angle=a)
canvas.after(2000, setric)
def delete():
canvas.delete("all")
a=0
setric()
canvas.bind('<Button-1>',delete)
Even when I change what is in the def delete I still get the error.
Thanks for your time! I am new to this so I have no idea where is the problem
....................................................................................................................................................................................................................................................

Functions called via bindings are automatically passed an object that represents the event. Your delete method needs to be able to accept this parameter even if you don't use it.
def delete(event):
canvas.delete("all")

Related

Is the a a way to bind the <BUTTON-1> event to a function when using tree view widget in python

treeView.bind('<Button-1>', select_item)
However, this code raises the below error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\OLWA EMMA\AppData\Lo
return self.func(*args)
File "main.py", line 118, in select
listOfUsers.append(str_lib['value
IndexError: string index out of range

How to fix "'int' object is not iterable" error in tkinter

I want to make an application, that uses Bash commands, but everytime it results in Exception in Tkinter callback.
I already have tried to use subprocess popen but there it even wanted to start the commands.
from tkinter import *
import os
# The following commands gets executed, if the user is pressing the action button
def button_action():
update_button.config(os.system('sudo -S pacman -Syu'), text="Aktualisiert!")
# create a window
fenster = Tk()
# set the title of the window
fenster.title("Befehlsammlung")
# create button and labels
update_label = Label(fenster, text="Drücke auf Aktualisieren, um alle Pakete zu updaten.",)
update_button = Button(fenster, text="Aktualisieren", command=button_action)
exit_button = Button(fenster, text="Schließen", command=fenster.quit)
# Add your components in your favourite
update_label.pack()
update_button.pack()
exit_button.pack()
# wait for input
fenster.mainloop()
I except the button changes to "Aktualisiert" and not the actual Exception error.
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.7/tkinter/__init__.py", line 106, in _cnfmerge
cnf.update(c)
TypeError: 'int' object is not iterable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/home/flozge/PycharmProjects/untitled1/GUI.py", line 7, in button_action
update_button.config(os.system('sudo -S pacman -Syu'), text="Aktualisiert!")
File "/usr/lib/python3.7/tkinter/__init__.py", line 1485, in configure
return self._configure('configure', cnf, kw)
File "/usr/lib/python3.7/tkinter/__init__.py", line 1469, in _configure
cnf = _cnfmerge((cnf, kw))
File "/usr/lib/python3.7/tkinter/__init__.py", line 109, in _cnfmerge
for k, v in c.items():
AttributeError: 'int' object has no attribute 'items'
Process finished with exit code 0
You don't need to pass the os.system('sudo -S pacman -Syu') call as an argument for the update_button.config for it to run. You just need to call it somewhere inside the button_action function, which will be triggered when "Aktalisieren" button is clicked as specified in line update_button = Button(fenster, text="Aktualisieren", command=button_action).
def button_action():
os.system('sudo -S pacman -Syu')
update_button.config(text="Aktualisiert!")

function inputs in classes

So i have started using classes in python and i am still learning how it works. so in this part of my code and i found something that i don't understand.
def Button(self):
self.Button = QPushButton("Push me Bitch!",self)
self.Button.setGeometry(QtCore.QRect(500, 500, 5, 5))
self.Button.resize(self.Button.minimumSizeHint())
self.Button.clicked.connect(self.ButtonPress(someInput))
# Do functions
def ButtonPress(self , someInput):
print('someInput')
self.OutputBox1.setText('I Has Been Pressed!')
And this is the error that i got:
Traceback (most recent call last):
File "C:\Users\Gebruiker\Desktop\Python shizzle\BijHillen\BijGui.py", line 92, in <module>
ex = Example()
File "C:\Users\Gebruiker\Desktop\Python shizzle\BijHillen\BijGui.py", line 40, in __init__
self.Button()
File "C:\Users\Gebruiker\Desktop\Python shizzle\BijHillen\BijGui.py", line 68, in Button
self.Button.clicked.connect(self.ButtonPress(5))
File "C:\Users\Gebruiker\Desktop\Python shizzle\BijHillen\BijGui.py", line 74, in ButtonPress
self.OutputBox1.setText('I Have Been Pressed!!!')
AttributeError: 'Example' object has no attribute 'OutputBox1'
If i remove the someInput. Then there is no problem. How does this work?
Best regards
Your event setup is wrong, when you attach an event to a button you generally want to link the function, not the result of the function to the clicked event.
Instead of:
self.Button.clicked.connect(self.ButtonPress(someInput))
You want:
self.Button.clicked.connect(self.ButtonPress)
That way the function will get called when the button is clicked.
You might have some other issues with your code, but start by fixing that.

Exception raised with trace_variable()

I have a function that is supposed to build a window according to which option in a dropdown menu is selected:
def buildview():
value = StringVar()
options = ["one", "two", "three"]
menu = OptionMenu(*(root, value) + tuple(options))
### Some window building accoring to the value selected... ###
value.trace_variable("w", buildview)
The exception that is raised looks like this (EDIT: Entire Traceback):
Traceback (most recent call last):
File "D:\Dropbox\PRO\infograbber\Infograbber 0.1.py", line 102, in <module>
mainloop()
File "C:\Python35\Python3564\lib\tkinter\__init__.py", line 405, in mainloop
_default_root.tk.mainloop(n)
File "C:\Python35\Python3564\lib\tkinter\__init__.py", line 1553, in __call__
self.widget._report_exception()
AttributeError: 'StringVar' object has no attribute '_report_exception'
What exactly is causing this? Can I not have a method call back itself like this? I don't even know where to start fixing this problem, so I'd appreciate any help.
I'm using Python 3.5 64 bit, Sublime Text 2, Windows 10.
EDIT:
Added a test callback function:
def test(*args):
print("test")
and changed the above trace to
value.trace_variable("w", test)
Now the exception changed to this:
Traceback (most recent call last):
File "C:\Python35\Python3564\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "D:\Dropbox\PRO\infograbber\Infograbber 0.1.py", line 56, in buildview
root.trace_variable("w", self.printcurrentarticle)
File "C:\Python35\Python3564\lib\tkinter\__init__.py", line 1948, in __getattr__
return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'trace_variable'
I'm not entirely sure if this is the only problem, but it's definitely a problem. When a trace fires it passes in three arguments. The function you've defined takes no arguments. You need to pass in a reference to a function that takes at least three arguments.
You also have the problem that each time the trace fires, you are creating another variable and another trace. On the surface that seems like a bad idea, unless you really do want to create a new option menu every time an optionmenu changes.

Pyglet file name "Resource not found"

I'm trying to open a music track and add it to a queue for a player in Pyglet.
def QueueAudio(self):
self.musicpath=filedialog.askopenfilename()
print(self.musicpath)
Player.queue(pyglet.resource.media(r"self.musicpath"))
The musicpath variable works fine as the print statement prints the filename. The error comes when the player tries to queue the track. Error below.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\site-packages\pyglet\resource.py", line 605, in media
location = self._index[name]
KeyError: 'self.musicpath'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
File "C:\Users\Rob\Google Drive\Coursework\Part 2\music player tests\test5.py", line 99, in QueueAudio
self.playerpath=pyglet.resource.media(r"self.musicpath")
File "C:\Python33\lib\site-packages\pyglet\resource.py", line 615, in media
raise ResourceNotFoundException(name)
pyglet.resource.ResourceNotFoundException: Resource "self.musicpath" was not found on the path. Ensure that the filename has the correct captialisation.
Does anyone know why this is, and what may fix it?
This line looks to be the culprit:
self.playerpath=pyglet.resource.media(r"self.musicpath")
You are passing a string "self.musicpath" to a function, when you should be passing in the contents of a variable named self.musicpath. You need to call it like this:
self.playerpath = pyglet.resource.media(self.musicpath)

Categories

Resources