How to fix "- _tkinter.TclError: no events specified in binding" - python

I found the source code somewhere on the internet
a simple program to create a window with the option
entering mathematical operations and displaying from the keyboard
the result.This is code:
import tkinter as tk
from math import *
def evaluate(event):
res.configure(text="Result: " + str(eval(entry.get())))
w = tk.Tk()
tk.Label(w, text="Your Expression:").pack()
entry = tk.Entry(w)
entry.bind("event", evaluate)
entry.pack()
res = tk.Label(w)
res.pack()
w.mainloop()
I'm getting an error:
C:\Users\rob\PycharmProjects\untitled2\venv\Scripts\python.exe
"C:/Users/rob/Desktop/new test.py" Traceback (most recent call last):
File "C:/Users/rob/Desktop/new test.py", line 12, in
entry.bind("", evaluate) File "C:\Users\rob\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py",
line 1248, in bind
return self._bind(('bind', self._w), sequence, func, add) File "C:\Users\rob\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py",
line 1203, in _bind
self.tk.call(what + (sequence, cmd))
_tkinter.TclError: no events specified in binding
Please help. Im starting learning Python and I don't know a solution.

it's clear in your reported error,
"...no events specified in binding"
change this
entry.bind('<event>', evaluate)
with this
entry.bind("<Return>",evaluate)

Related

I am getting an error with tkinter ' bad window path name .!button2 ' and I am not sure why?

I am making a password management system in Tkinter but while deleting an entry I am getting this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Users/user/opt/anaconda3/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
return self.func(*args)
File "/Users/user/opt/anaconda3/lib/python3.8/site-packages/tkmacosx/basewidget.py", line 1094, in cmd
self.cnf['command']()
File "main.py", line 149, in delete
i.destroy()
File "main.py", line 169, in destroy
self.showButton.destroy()
File "/Users/user/opt/anaconda3/lib/python3.8/site-packages/tkmacosx/variables.py", line 55, in _patch
return fn(self)
File "/Users/user/opt/anaconda3/lib/python3.8/site-packages/tkmacosx/basewidget.py", line 1313, in destroy
main_win = self.winfo_toplevel()
File "/Users/user/opt/anaconda3/lib/python3.8/tkinter/__init__.py", line 1223, in winfo_toplevel
return self._nametowidget(self.tk.call(
_tkinter.TclError: bad window path name ".!button2"
this is a snippet from the code where I am getting an error:
def delete(self):
row = self.deleteButton.grid_info()['row'] # this will get the row you want to delete
ask = messagebox.askquestion("Are You Sure", "are you sure you want to delete this?")
if ask == "yes":
for i in objects: # TODO: cannot delete more than one entry after logging in.
i.destroy()
file = open('app_manager.txt', 'r')
lines = file.readlines()
file.close()
del lines[row - 6] # this will delete the data-entry from 'app_manager.txt'
file = open("app_manager.txt", "w")
for line in lines:
file.write(line)
file.close()
readfile()
NOTE: I can delete one entry but if I try and delete another entry then it shows the error I mentioned above.
If you need full code to inspect then this is the link:
https://github.com/vendz/Account-Storage-System/blob/master/main.py
The problem was with from tkmacosx import Button so instead use from tkinter import Button.
EDIT: problem with tkmacosx is now resolved. do update the package

tkinter image display shows TclError error

I want to simply display a logo or image in my GUI. I tried different techniques as posted on the internet, however all seems to give me an error as TclError: image "pyimage27" doesn't exist. I have checked the file is correct(png file) and the path too. I still cant display the image. Below is my simple code:
import tkinter as tk
root = tk.Tk()
def show_image():
img_label = tk.Label(root)
img_label.image = tk.PhotoImage(file="logo.png")
img_label['image'] = img_label.image
img_label.pack()
show_image()
root.mainloop()
Error that i get
File "C:\Users\bb\Documents
\line.py", line 54, in <module>
alien()
File "C:\Users\bb\Documents
\test.py\line.py", line 26, in __init__
self.alien2 = self.canvas.create_image((100,100),image=CardVar,anchor=CENTER)
File "C:\Users\bb\Anaconda3\lib\tkinter\__init__.py", line 2489, in create_image
return self._create('image', args, kw)
File "C:\Users\bb\Anaconda3\lib\tkinter\__init__.py", line 2480, in _create
*(args + self._options(cnf, kw))))
TclError: image "pyimage30" doesn't exist
Thank you
Many similar answers are available but that did not help :
When using Tkinter, error: TclError: image "pyimage8" doesn't exist
Python: Tkinter TclError: can't invoke "image" command
You can try with PhotoImage(file="path", master=root)

tkinter.TclError: invalid command name "table"

Okay, first of all I don't know what is the actual problem here so I couldn't come up with a more accurate title. Maybe some of you can edit it to make it accurate
The following is the minimalised code to reproduce the problem I'm having.
from traybar import SysTrayIcon
from cal import Calendar
import Tkinter
class Add():
def __init__(self,master):
Calendar(master).pack()
def add(systray):
root = Tkinter.Tk()
Add(root)
root.mainloop()
SysTrayIcon("abc.ico","abc", (('Add',None, add), ) ,default_menu_index=0).start()
The cal and the trabar are these files http://tkinter.unpythonic.net/wiki/TkTableCalendar and https://github.com/Infinidat/infi.systray/blob/develop/src/infi/systray/traybar.py respectively.
If you run this, it will make a icon in the system tray of a windows machine with the options Add and Quit. clicking on the app opens up the calender, no problem. Close the calender and click on the Add again. But this time it doesn't open the calendar and throws the following error
`
Traceback (most recent call last):
File "_ctypes/callbacks.c", line 314, in 'calling callback function'
File "C:\Users\Koushik Naskar\AppData\Roaming\Python\Python27\site-packages\traybar.py", line 79, in WndProc
self._message_dict[msg](hwnd, msg, wparam.value, lparam.value)
File "C:\Users\Koushik Naskar\AppData\Roaming\Python\Python27\site-packages\traybar.py", line 276, in _command
self._execute_menu_option(id)
File "C:\Users\Koushik Naskar\AppData\Roaming\Python\Python27\site-packages\traybar.py", line 283, in _execute_menu_option
menu_action(self)
File "C:\Users\Koushik Naskar\Desktop\So\temp.py", line 11, in add
Add(root)
File "C:\Users\Koushik Naskar\Desktop\So\temp.py", line 7, in __init__
Calendar(master).pack()
File "C:\Users\Koushik Naskar\Desktop\So\cal.py", line 66, in __init__
state='disabled', browsecommand=self._set_selection)
File "C:\Python27\lib\lib-tk\tktable.py", line 118, in __init__
Tkinter.Widget.__init__(self, master, 'table', kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2090, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: invalid command name "table"
`
This problem only appears when I use SysTrayIcon with the Calendar.Instead of Calendar if you use simple Tkinter Button or Label etc. this error doesn't appear. Also I can use the Calendar widget normally (without the SysTrayIcon ) in a usual Tkinter GUI as many times as I want, no error occur there. Now I don't have any clue about whats happenning here and how to fix this. What problem does SysTrayIcon have with Calendar and Why the error doesn't happen the first time I open the GUI? Please help.
TkTableCalendar requies the tktable module, which you have installed in lib-tk (3rd party modules usually go into lib/site-packages) as indicated by this part of the traceback.
File "C:\Python27\lib\lib-tk\tktable.py", line 118, in __init__
Tkinter.Widget.__init__(self, master, 'table', kw)
The tktable module requires that your tcl/tk installation have the tktable extension. That extension defines the 'table' widget. It is not part of the standard tcl/tk that is installed with Python on Windows. Hence
_tkinter.TclError: invalid command name "table"
The tktable source code (probably a mixture of tcl and C) is hosted at SourceForge. This page says that it is part of the ActiveState Batteries Included distribution. I don't know if the free version of AS tcl/tk has all the 'batteries'. And I don't know how to replace the tcl/tk installation you already have with a new one. I personally would use an alternative if possible.

Huge error message when using .config

I'm trying to learn tkinter and am making a calculator.
I am setting up the buttons and am now trying to change the size of one of them with:
Button_back = ttk.Button(Frame1, text='Back', command=printhi) #printhi is temporary.
Button_back.grid(column=0, row=0)
Button_back.config( height = 25, width = 25 )
When I try run it the error message is:
>Traceback (most recent call last): File "C:\Users\Luuk\Python
>PGMs\tkinter\2-1 - Calculator.py", line 75, in <module>
>Button_back.config( Height = 25, width = 25 )
>File "C:\Program Files (x86)\Python34\lib\tkinter\__init__.py", line 1270, in configure
>return self._configure('configure', cnf, kw) File "C:\Program Files (x86)\Python34\lib\tkinter\__init__.py", line 1261, in
> _configure
>self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
> _tkinter.TclError: unknown option "-height"
I have no idea what I'm doing wrong here, as per every webpage this is what should handle the width and height correctly
Your answer is half right...but there is a height option for a Tkinter Button, just not a ttk Button. If you absolutely need the height option, you can still use a regular Tkinter button, it just won't be as pretty. Also, you can print the dictionary of widget options if you're ever in this bind again by printing widget.config().
import tkinter as tk
import ttk
root = tk.Tk()
tk_button = tk.Button(root, text='tkinter button')
ttk_button = ttk.Button(root, text='ttk button')
for key in tk_button.config().iterkeys():
print('tkinter: ' + key)
for key in ttk_button.config().iterkeys():
print('ttk: ' + key)
tk_button.pack()
ttk_button.pack()
root.mainloop()
There is no such thing as 'height' for a button, only width... (Also for other things probably but this is what I know for sure now)
Found this after another half hour search:
http://www-acc.kek.jp/WWW-ACC-exp/KEKB/control/Activity/Python/TkIntro/introduction/button.htm

How do I change the background of a Frame in Tkinter?

I have been creating an Email program using Tkinter, in Python 3.3.
On various sites I have been seeing that the Frame widget can get a different background using Frame.config(background="color").
However, when I use this in my Frames it gives the following error:
_tkinter.TclError: unknown option "-Background"
It does not work when doing the following:
frame = Frame(root, background="white")
Or:
frame = Frame(root)
frame.config(bg="white")
I can't figure it out.
I would post my whole source code but I dont want it exposed on the internet, but the frame creation goes something like this:
mail1 = Frame(self, relief=SUNKEN)
mail1.pack()
mail1.place(height=70, width=400, x=803, y=109)
mail1.config(Background="white")
I have tried multiple options trying to modify the background. The frame is like a wrap around an email preview for an inbox.
In case it's needed, this the way I am importing my modules:
import tkinter, time, base64, imaplib, smtplib
from imaplib import *
from tkinter import *
from tkinter.ttk import *
The following is the full traceback:
Traceback (most recent call last):
File "C:\Users\Wessel\Dropbox\Python\Main\Class Ginomail.py", line 457, in <module>
main()
File "C:\Users\Wessel\Dropbox\Python\Main\Class Ginomail.py", line 453, in main
app = Application(root) #start the application with root as the parent
File "C:\Users\Wessel\Dropbox\Python\Main\Class Ginomail.py", line 60, in __init__
self.initINBOX()
File "C:\Users\Wessel\Dropbox\Python\Main\Class Ginomail.py", line 317, in initINBOX
mail1.config(bg="white")
File "C:\Python33\lib\tkinter\__init__.py", line 1263, in configure
return self._configure('configure', cnf, kw)
File "C:\Python33\lib\tkinter\__init__.py", line 1254, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-bg"
Gives the following error with the code from the answer:
File "C:\Users\Wessel\Dropbox\Python\Main\Class Ginomail.py", line 317, in initINBOX
mail1 = Frame(self, relief=SUNKEN, style='myframe')
File "C:\Python33\lib\tkinter\ttk.py", line 733, in __init__
Widget.__init__(self, master, "ttk::frame", kw)
File "C:\Python33\lib\tkinter\ttk.py", line 553, in __init__
tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "C:\Python33\lib\tkinter\__init__.py", line 2075, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: Layout myframe not found
Solved! Thanks. Its the inbox bar to the right, background needed to be white.
The root of the problem is that you are unknowingly using the Frame class from the ttk package rather than from the tkinter package. The one from ttk does not support the background option.
This is the main reason why you shouldn't do wildcard imports -- you can overwrite the definition of classes and commands.
I recommend doing imports like this:
import tkinter as tk
import ttk
Then you prefix the widgets with either tk or ttk :
f1 = tk.Frame(..., bg=..., fg=...)
f2 = ttk.Frame(..., style=...)
It then becomes instantly obvious which widget you are using, at the expense of just a tiny bit more typing. If you had done this, this error in your code would never have happened.
You use ttk.Frame, bg option does not work for it. You should create style and apply it to the frame.
from tkinter import *
from tkinter.ttk import *
root = Tk()
s = Style()
s.configure('My.TFrame', background='red')
mail1 = Frame(root, style='My.TFrame')
mail1.place(height=70, width=400, x=83, y=109)
mail1.config()
root.mainloop()

Categories