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
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.
I am trying to make a date picker with tkcalendar, but everytime I try to run it, its always show me this error
Traceback (most recent call last):
File "C:\Python\Django\UJT\test.py", line 1, in <module>
from tkcalendar import *
File "C:\Python\venv\lib\site-packages\tkcalendar\__init__.py", line 26, in <module>
from tkcalendar.dateentry import DateEntry
File "C:\Python\venv\lib\site-packages\tkcalendar\dateentry.py", line 35, in <module>
from tkcalendar.calendar_ import Calendar
File "C:\Python\venv\lib\site-packages\tkcalendar\calendar_.py", line 35, in <module>
from babel import default_locale
File "C:\Python\venv\lib\site-packages\babel\__init__.py", line 29, in <module>
from babel.core import *
File "C:\Python\venv\lib\site-packages\babel\core.py", line 19, in <module>
from babel import localedata
File "C:\Python\venv\lib\site-packages\babel\localedata.py", line 26, in <module>
from UserDict import DictMixin
ModuleNotFoundError: No module named 'UserDict'
Process finished with exit code 1
I have already installed tkcalendar and update my pip, but I still get the error.
Here's my code
from tkcalendar import *
from tkinter import *
root = Tk()
cal = Calendar(root, width=12, year=2019, month=6, day=22,
bg='white', fg='black', borderwidth=2)
cal.pack(padx=10, pady=10)
root.mainloop()
please help me, thankyou
This is the Code that I am trying to run:
from tkinter import *
from PIL import ImageTk,Image
root = Tk()
root.title("GUI with Images")
root.iconbitmap('icon.png')
button_quit = Button(root, text=("Exit Program"), command = root.quit)
button_quit.pack()
my_img = ImageTk.PhotoImageTk(Image.open("icon.png"))
my_label = Label(image=my_img)
my_label.pack()
root.mainloop()
And this is the Error Showing:
Traceback (most recent call last):
File
"F:\Pycharm\PracticeProjects\PythonPractice\PythonApplication1\images.py",
line 2, in
from PIL import ImageTk,Image
File "F:\Pycharm\Python\lib\site-packages\PIL\ImageTk.py", line 31,
in
from . import Image
File "F:\Pycharm\Python\lib\site-packages\PIL\Image.py", line 109, in
from . import _imaging as core
ImportError: cannot import name '_imaging' from 'PIL'
(F:\Pycharm\Python\lib\site-
packages\PIL_init_.py)
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.
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.