Traceback (most recent call last):
File "/Users/me/Documents/dev/myui/myui.py", line 134, in <module>
myCanvas = ttk.Canvas(myFrame, width=root.winfo_width(), height=30, borderwidth=1, padding='0 1 0 1', style='myCanvas.TFrame')
AttributeError: 'module' object has no attribute 'Canvas'
This tells me there is no such a Canvas class with themed-TK. How do I use canvas with ttk then? Just the same old tk.Canvas?
I'm running native Python 2.7 on Mac OS X El Capitan.
There is no ttk canvas. You can use the Canvas widget that is part of Tkinter.
Related
My code stopped working quite literally overnight, due to an issue with Tkinter.
I'm using PySimpleGui for my project, which was working the previous day.
When I run my program it showed the bellow error:
File "E:/Projekty Python/dcs_assistant/gui_and_video.py", line 1, in <module>
import PySimpleGUI as sg
File "C:\Users\pawni\Miniconda3\envs\dcs_assistant\lib\site-packages\PySimpleGUI\__init__.py", line 2, in <module>
from .PySimpleGUI import *
File "C:\Users\pawni\Miniconda3\envs\dcs_assistant\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 125, in <module>
tclversion_detailed = tkinter.Tcl().eval('info patchlevel')
File "C:\Users\pawni\Miniconda3\envs\dcs_assistant\lib\tkinter\__init__.py", line 2354, in __getattr__
return getattr(self.tk, attr)
AttributeError: module '_tkinter' has no attribute 'eval'
Process finished with exit code 1
Line 125 is this:
tclversion_detailed = tkinter.Tcl().eval('info patchlevel')
This, as per Tkinter documentation, should simply return the version of Tkinter library (https://tkdocs.com/tutorial/install.html).
When I ran a dummy program using just Tkinter to test if Tkinter alone works. I got another error:
Traceback (most recent call last):
File "E:/Projekty Python/dcs_assistant/test.py", line 3, in <module>
tk = tkinter.Tk()
File "C:\Users\pawni\Miniconda3\envs\dcs_assistant\lib\tkinter\__init__.py", line 2272, in __init__
self._loadtk()
File "C:\Users\pawni\Miniconda3\envs\dcs_assistant\lib\tkinter\__init__.py", line 2286, in _loadtk
tk_version = self.tk.getvar('tk_version')
AttributeError: module '_tkinter' has no attribute 'getvar'
Process finished with exit code 1
The code for the dummy program was:
import tkinter
from tkinter.constants import *
tk = tkinter.Tk()
frame = tkinter.Frame(tk, relief=RIDGE, borderwidth=2)
frame.pack(fill=BOTH,expand=1)
label = tkinter.Label(frame, text="Hello, World")
label.pack(fill=X, expand=1)
button = tkinter.Button(frame, text="Exit", command=tk.destroy)
button.pack(side=BOTTOM)
tk.mainloop()
I tried to uninstall and re-install Tkinter again using both pip and conda. No joy.
The program:
from tkinter import *
root = Tk()
canvas = Canvas(root)
canvas.pack()
root.mainloop()
error for this program:
c:/Users/ADMIN/Desktop/python/tkinter.py
Traceback (most recent call last):
File "c:\Users\ADMIN\Desktop\python\tkinter.py", line 1, in <module>
from tkinter import *
File "c:\Users\ADMIN\Desktop\python\tkinter.py", line 3, in <module>
root = Tk()
NameError: name 'Tk' is not defined
is the problem I am still facing in my computer. please help me to rectify this error
I am trying to display an image in a GUI, and don't understand what is wrong. I keep getting this error:
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
What should my code (especially the line with my_img=...) look like?
My Code:
from tkinter import *
from PIL import ImageTk,Image
my_img = ImageTk.PhotoImage(Image.open("iu.jpeg"))
my_label = Label(image=my_img)
my_label.pack()
root = Tk()
root.title("ICON PRACTICE")
root.iconbitmap('iu.ico')
button_quit = Button(root, text = "EXIT", command=root.quit)
button_quit.pack()
root.mainloop()
The full error
Traceback (most recent call last):
File "main.py", line 4, in <module>
my_img = ImageTk.PhotoImage(Image.open("test.png"))
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/PIL/ImageTk.py", line 112, in __init__
self.__photo = tkinter.PhotoImage(**kw)
File "/usr/lib/python3.8/tkinter/__init__.py", line 4064, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/usr/lib/python3.8/tkinter/__init__.py", line 3997, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image
Exception ignored in: <function PhotoImage.__del__ at 0x7f7148fadc10>
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/PIL/ImageTk.py", line 118, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
Try doing this, its probably because you create the root object after opening the image and creating the photo object.
import os
from tkinter import *
from PIL import ImageTk,Image
root= Tk()
i = Image.open("C:/path/to/the/image/directory/image.png")
photo = ImageTk.PhotoImage(i)
root.mainloop()
The Label widget will only work after root = Tk() (Tk() starts the underlying Tcl interpreter) is declared. Then, all child widgets must have root as their first parameter (e.g., Label(root, text='hi')). You started the interpreter after you tried to use it, so Python raised an exception.
I've looked around a bit, but I can't find an answer to my error. Here is the code:
import tkinter as tk
root=tk.Tk()
class Page(tk.Frame):
'''Enables switching between pages of a window.'''
def __init__(self):
self.widgets={}
self.grid(column=0,row=0)
page=Page()
tk.mainloop()
Here is the error:
Traceback (most recent call last):
File "C:\Documents and Settings\Desktop\Python Scripts\Tkinter.py", line 11, in <module>
page=Page()
File "C:\Documents and Settings\Desktop\Python Scripts\Tkinter.py", line , in __init__
self.grid(column=0,row=0)
File "C:\Python34\lib\tkinter\__init__.py", line 2055, in grid_configure
self.tk.call(
AttributeError: 'Page' object has no attribute 'tk'
I'm fairly new to tkinter, and this error has me stumped. I'd really appreciate any help, thank you!
Your Page init method should call Frame's init.
class Page(tk.Frame):
'''Enables switching between pages of a window.'''
def __init__(self):
super(Page, self).__init__()
self.widgets={}
self.grid(column=0,row=0)
(Python version: 3.1.1)
I am having a strange problem with StringVar in tkinter. While attempting to continuously keep a Message widget updated in a project, I kept getting an error while trying to create the variable. I jumped out to an interactive python shell to investigate and this is what I got:
>>> StringVar
<class 'tkinter.StringVar'>
>>> StringVar()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python31\lib\tkinter\__init__.py", line 243, in __init__
Variable.__init__(self, master, value, name)
File "C:\Python31\lib\tkinter\__init__.py", line 174, in __init__
self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
>>>
Any ideas? Every example I have seen on tkinter usage shows initializing the variable with nothing sent to the constructor so I am at a loss if I am missing something...
StringVar needs a master:
>>> StringVar(Tk())
<Tkinter.StringVar instance at 0x0000000004435208>
>>>
or more commonly:
>>> root = Tk()
>>> StringVar()
<Tkinter.StringVar instance at 0x0000000004435508>
When you instantiate Tk a new interpreter is created. Before that nothing works:
>>> from Tkinter import *
>>> StringVar()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Python26\lib\lib-tk\Tkinter.py", line 251, in __init__
Variable.__init__(self, master, value, name)
File "C:\Python26\lib\lib-tk\Tkinter.py", line 182, in __init__
self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
>>> root = Tk()
>>> StringVar()
<Tkinter.StringVar instance at 0x00000000044C4408>
The problem with the examples you found is that probably in the literature they show only partial snippets that are supposed to be inside a class or in a longer program so that imports and other code are not explicitly indicated.