I'm trying to write a tkinter program, and I came across an error that I've never seen before while working with tkinter. I've searched around everywhere, and tried everything I can think of. This is my code so far:
x=tk.Canvas(top,width=1000,height=750,bg="grey")
x.pack()
y=tk.PhotoImage(file="C:\\Users\\Admin\\Desktop\\images (3)_CnyokaDvJmG1xu.png")
x.create_image(top,0,0,image=y)`
and this is error
Traceback (most recent call last):
File "C:\Users\Admin\Anaconda3\lib\tkinter\__init__.py", line
1705, in __call__
return self.func(*args)
File "C:/Users/Admin/.spyder-py3/temp.py", line 16, in open
x.create_image(top,0,0,image=y).pack()
File "C:\Users\Admin\Anaconda3\lib\tkinter\__init__.py", line
2489, in create_image
return self._create('image', args, kw)
File "C:\Users\Admin\Anaconda3\lib\tkinter\__init__.py", line
2480, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: bad screen distance "."
You don't have to supply containing widget when placing an image in a canvas, coordinates are enough:
x.create_image(0,0,image=y)
The error is because canvas does not accept a widget as coordinates.
Related
I am searching any simple way to make urwid.Frame work in python without success, as example, I try this MWE:
frame = urwid.Frame(urwid.Text(('some text'), align='center'))
filler = urwid.Filler(frame, "top")
loop = urwid.MainLoop(filler)
loop.run()
And I get the following unuseful error message:
Traceback (most recent call last):
File "pycurses.py", line 596, in <module>
loop.run()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 287, in run
self._run()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 385, in _run
self.event_loop.run()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 790, in run
self._loop()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 818, in _loop
self._entering_idle()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 779, in _entering_idle
callback()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 574, in entering_idle
self.draw_screen()
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/main_loop.py", line 588, in draw_screen
canvas = self._topmost_widget.render(self.screen_size, focus=True)
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/widget.py", line 145, in cached_render
canv = fn(self, size, focus=focus)
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/decoration.py", line 814, in render
top, bottom = self.filler_values(size, focus)
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/decoration.py", line 799, in filler_values
height = self._original_widget.rows((maxcol,),focus=focus)
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/widget.py", line 205, in cached_rows
return fn(self, size, focus)
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/container.py", line 1611, in rows
return sum(self.get_item_rows(size, focus))
File "/home/fauve/.local/lib/python3.5/site-packages/urwid/container.py", line 1512, in get_item_rows
l.append(w.rows((maxcol,),
AttributeError: 'Frame' object has no attribute 'rows'
I try many variations, such putting the frame inside an urwid.Pile before giving it to the Filler widget, but I still get similar error messages.
So, is their any minimalist example work using urwid.Frame?
And what doese mean this “Frame' object has no attribute 'rows'”? I never ask in my example any .rows method, so why the error message have a behavior like if I used it?
The error is indeed not very good. The actual problem here is that the nesting of the Frame and the Filler should be the other way around:
import urwid
text = urwid.Text('some text', align='center')
filler = urwid.Filler(text, "top")
frame = urwid.Frame(filler)
loop = urwid.MainLoop(frame)
loop.run()
A flow widget (like Text) gets to decide how many rows it should be. The main loop expects a box widget (which does not get to pick its own width or height). The Filler widget bridges the two: it asks the wrapped flow widget how many rows it takes up (given a number of columns) and then fills up the rest of the rows.
The Frame is a box widget that contains another box widget in the center.
If you haven't seen this page already, I highly recommend looking at the diagrams here: http://urwid.org/manual/widgets.html#included-widgets The colors describe what kinds of widgets nest inside what kinds of widgets.
(Also I suspect it would be technically feasible for Urwid to detect that the widget is of the wrong type and say that instead in the error message, but I haven't dived that deep into the implementation.)
Althogh it works perfectly fine with built in turtle shapes it doesn't work with
new registered shapes.The error is pyimage1 doesn't exist and both my program and file are in the same directories
Here is the code
root=Tk()
import turtle
def image():
global img
img='batman.gif'
player=turtle.Turtle()
wn=turtle.Screen()
wn.register_shape(img)
player.shape(img)
B=Button(root,text='click',command=image).pack()```
The error shown is:```Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\dell\OneDrive\Desktop\imagetk.py", line 10, in image
player.shape(img)
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 2777, in shape
self.turtle._setshape(name)
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 2506, in _setshape
self._item = screen._createimage(screen._shapes["blank"]._data)
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\turtle.py", line 723, in _createimage
return self.cv.create_image(0, 0, image=image)
File "<string>", line 1, in create_image
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 2489, in create_image
return self._create('image', args, kw)
File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 2480, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: image "pyimage1" doesn't exist```
The problem is the way you're mixing turtle and tkinter is creating two roots which leads to this error. You're trying to use standalone turtle when you should be using embedded turtle. I.e. you should be using RawTurtle instead of Turtle and RawScreen instead of Screen. But don't just swap the names, look them up in the documentation. Your code should look roughly like:
from tkinter import *
from turtle import TurtleScreen, RawTurtle
IMAGE = 'batman.gif'
def image():
player = RawTurtle(screen)
player.shape(IMAGE)
root = Tk()
Button(root, text='click', command=image).pack()
canvas = Canvas(root)
canvas.pack()
screen = TurtleScreen(canvas)
screen.register_shape(IMAGE)
screen.mainloop()
I write on Python an GUI with dynamic resizing, I mean, if the height of all the elements of the window are more than the height of the window, the scrollbar appears on the right side, but if I resize the window and the height of all elements are less than the window, the scrollbar disappear.
I have made this using the <Configure> event. This is part of the code I have used:
mycanvas = Canvas(self.parent,bd=0,highlightthickness=0,relief='flat')
sbar = tk.Scrollbar(mycanvas,orient='vertical')
sbar.config(command=mycanvas.yview)
mycanvas.config(yscrollcommand=sbar.set)
def canvasscroll(event):
mycanvas.yview('scroll',int(-1*(event.delta/120)), "units")
def resizecanvas(event):
if self.mainyposition > event.height: #mainyposition is the addition of the height of all widgets in screen
sbar.pack(side=RIGHT, fill=Y)
else:
sbar.forget()
print(event.height)
self.widgetname.bind_all("<MouseWheel>", canvasscroll)
self.widgetname.bind_all("<Configure>", resizecanvas)
Everything works well except for two issues:
First of all, when the window starts, the <Configure> event is called a few times, and print the height on screen, but the last time its called before the program stills on stand by is a height that is not the correct height of the window, so this causes that it shows unnecesarily the scrollbar. The problem solves itself once I resize the window and the event runs again, but the first run it fails doing this. Any way to solve this?
And second, during the resizing, the console doesn't show any errors, but when I close the window, it shows me an Exception in Tkinter Callback that references the pack() method of the scrollbar. I don't know how to solve this... The errors are this:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "Y:\Factory\GLP2-e Admin (Todos los archivos)\Nueva Interfaz\classmain.py", line 108, in resizecanvas
sbar.pack(side=RIGHT, fill=Y)
File "C:\Python34\lib\tkinter\__init__.py", line 2140, in pack_configure + self._options(cnf, kw))
_tkinter.TclError: bad window path name ".!canvas.!scrollbar"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "Y:\Factory\GLP2-e Admin (Todos los archivos)\Nueva Interfaz\classmain.py", line 108, in resizecanvas
sbar.pack(side=RIGHT, fill=Y)
File "C:\Python34\lib\tkinter\__init__.py", line 2140, in pack_configure + self._options(cnf, kw))
_tkinter.TclError: bad window path name ".!canvas.!scrollbar"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "Y:\Factory\GLP2-e Admin (Todos los archivos)\Nueva Interfaz\classmain.py", line 108, in resizecanvas
sbar.pack(side=RIGHT, fill=Y)
File "C:\Python34\lib\tkinter\__init__.py", line 2140, in pack_configure + self._options(cnf, kw))
_tkinter.TclError: bad window path name ".!canvas.!scrollbar"
[Finished in 22.3s]
Thanks to everyone.
I want to display an icon on a menu bar, so using this information, I coded this:
img = Image.open("help.png")
menubar.add_cascade(label="Help",menu=helpmenu,bitmap=ImageTk.PhotoImage(img))
I got this error:
Traceback (most recent call last):
File "mine.py", line 67, in <module>
m.menus(root)
File "mine.py", line 55, in menus
menubar.add_cascade(label="Help",menu=helpmenu,bitmap=ImageTk.PhotoImage(img))
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2699, in add_cascade
self.add('cascade', cnf or kw)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2696, in add
self._options(cnf, kw))
_tkinter.TclError: bitmap "pyimage2" not defined
How to fix this ?
To display a PhotoImage you should use the image attribute, not bitmap.
You can also simply open an image file directly using ImageTk.PhotoImage(file='...')
So you can use the following code to display your image in the menu:
img = ImageTk.PhotoImage(file="help.png")
menubar.add_cascade(label="Help", menu=helpmenu, image=img)
Here I'm starting to create a TK window with a text field but when ever I run this I get the error
Exception in Tkinter callback
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1470, in __call__
return self.func(*args)
File "School.py", line 31, in begin
emulatorI=emulator()
File "School.py", line 20, in __init__
code.pack(self.root)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1868, in pack_configure
+ self._options(cnf, kw))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1105, in _options
cnf = _cnfmerge(cnf)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 114, in _cnfmerge
for c in _flatten(cnfs):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1826, in __getattr__
return getattr(self.tk, attr)
AttributeError: __len__
from within the module. What am I doing wrong? I'm realitively new to classes so I may have done something wrong in the class setup.
I'm running python 2.7 on OS 10.9
class emulator:
def __init__(self):
self.root=Tk()
self.root.geometry("500x500")
self.root.title("Python")
code=Text(self.root)
code.pack(self.root)
self.root.mainloop()
emulatorI=emulator()
The problem is with the line
code.pack(self.root)
The pack function normally takes no arguments except keyword arguments, and so should just be called as
code.pack()
The reason for the very odd error is that pack can take a positional argument, which is expected to be a dictionary of options. When trying to treat the Tk instance as a dictionary, it failed due to the lack of a __len__ method.
What is the purpose of code.pack(self.root)?
The arguments for pack are placement directions, such as side, padding, fill, anchor, etc
(see http://effbot.org/tkinterbook/pack.htm ).