So guys that's a code i tried to create a star fractal but i found some errors. That's my code:
https://www.hsslive.co.in/2021/07/how-to-draw-a-star-using-python.html it's the one using tkinter not turtle and when i run it in python it says:
Canvas.create_text(150,150,text='pentagram')
File "C:\Users\Stefa\AppData\Local\Programs\Python\Python310\lib\tkinter_init_.py", line 2839, in create_text
return self._create('text', args, kw)
AttributeError: 'int' object has no attribute '_create
Related
Cannot seem to figure out the below Tkinter message. Usually doing a quick
search will provide answers but this time I seem to miffed the search engines as to
what might be causing the below error. Curious to know if I am missing a Python package or line 25 below is used in an older version of Python and it has been updated to
a newer command.
I am importing the following packages into the script:
from tkinter import *
from tkinter import filedialog
The function is suppose to save any typed text put into a text area. It does save the file but the file is empty.
Thanks,
Kurt
C:\Users\kurt>python --version
Python 3.10.4
def saveFiles():
filename = filedialog.asksaveasfile(
mode='w',
title="Save a File",
defaultextension=".txt"
)
filename.config(mode='w') ------------> **This is line 25**
pathh.insert(END, filename)
data = str(txtarea.get(1.0, END))
filename.write(data)
filename.close()
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "c:\Users\kurt\Documents\Scripts\TKinter\fileExplorerReadFile.py", line 25, in saveFiles
filename.config(mode='w')
AttributeError: '_io.TextIOWrapper' object has no attribute 'config'
The error is saying that the object returned by the asksaveasfile method doesn't have a config method.
tkinter.filedialog.asksaveasfile(mode='w', **options)ΒΆ
Create a SaveAs dialog and return a file object opened in write-only mode.
When you call the asksaveasfile method, it automatically returns a file object in write mode already so there is no need for any further configuration to write to the file. If you were to omit the line throwing the error, your code should work the way you intended.
I am trying to the following code in python 3.7.3 with PIL version 6.2.1:
render = ImageTk.PhotoImage(Image.open(pic))
but it results in an error message like the following:
Traceback (most recent call last):
File "F:/python/test/test10.py", line 12, in <module>
render = ImageTk.PhotoImage(Image.open(pic))
File "C:\Users\erica\AppData\Roaming\Python\Python37\site-packages\PIL\ImageTk.py", line 118, in __init__
self.__photo = tkinter.PhotoImage(**kw)
File "C:\Users\erica\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 3545, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\erica\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 3489, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image
Exception ignored in: <function PhotoImage.__del__ at 0x0000027A91FEB0D0>
Traceback (most recent call last):
File "C:\Users\erica\AppData\Roaming\Python\Python37\site-packages\PIL\ImageTk.py", line 124, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
I have tried different Pillow versions, tried inputting class instance as suggested from other posts, tried using os.chdir(pic_dir). But they all don't work.
Using the ImageTk module depends on a Tkinter instance because ImageTk.PhotoImage is designed to be "used everywhere Tkinter expects an image object".
From the Traceback, PhotoImage basically just calls Tkinter's PhotoImage constructor:
self.__photo = tkinter.PhotoImage(**kw)
then the base class for PhotoImage checks for a running Tkinter instance:
def __init__(self, imgtype, name=None, cnf={}, master=None, **kw):
self.name = None
if not master:
master = _default_root
if not master:
raise RuntimeError('Too early to create image')
and since it doesn't find one, it raises that "Too early to create image" error. Then in PIL, it just ignores that error ("Exception ignored in:...") so the rest of the creation of PhotoImage fails with the error you are getting.
To solve this, the Tkinter parts have to be properly initialized.
Try creating a Tkinter instance first:
from PIL import ImageTk, Image
from tkinter import Tk
root = Tk()
render = ImageTk.PhotoImage(image=Image.open("sample.jpg"))
Or use the generic Image module that does not depend on Tkinter.
self.img = ImageTk.PhotoImage(Image.open("yourimage.png")) # photoimage attribute error .......
img = Label(self.root,image = self.img).place(x=0,y=0,relheight=1,relwidth=1)
#love man
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.
I'm using python's Turtle Graphics and I want to set a background picture. However, it's not working. I need an answer quick as I have an assignment for my Computer Science class tomorrow >_<. Here's my code:
import time
import sys
import turtle
##Render
turtle.bgpic("background.png")
##End
turtle.done()
And I'm getting this error:
Traceback (most recent call last):
File "C:/Users/Alfie/Desktop/Youtube Game/Youtube.py", line 6, in <module>
turtle.bgpic("background.png")
File "<string>", line 8, in bgpic
File "C:\Python27\lib\lib-tk\turtle.py", line 1397, in bgpic
self._bgpics[picname] = self._image(picname)
File "C:\Python27\lib\lib-tk\turtle.py", line 503, in _image
return TK.PhotoImage(file=filename)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 3366, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 3320, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: couldn't recognize data in image file "background.png"
Followed with a Not Responding screen. Anyone know what the error is?
Tk only supports GIF, PGM and PPM according to this question. Your turtle library uses Tk internally and so you have to use a GIF file for your background.
I have tried that function.
I have used format .png --> OK
Remember \\ instead of \
ex: turtle.bgpic("""C:\\Users\\ASUS\\Downloads\\test.gif""")
--> So I think at that time, we can use .png in Python turtle.
I have a problem using tkinter canvas object. The problem lies in the following function, and more precisely in self.canvas.destroy(). When i comment out the try-statement the function works perfectly for about 10000 cycles, then tkinter stops working altogether, which I suspect has to do with memory errors from not deleting the canvas object.
def updateFrame(self):
self.canvas.pack_forget()
try:
self.canvas.destroy()
except:
pass
self.canvas = Canvas(self.root, width = 640, height = 640, bg = 'black')
for player in self.players:
player.drawMaster()
self.canvas.pack()
self.canvas.after(10, self.updateFrame)
However, the error message I get when self.canvas.destroy() is active comes after two cycles.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1442, in __call__
return self.func(*args)
File "C:\Python33\lib\tkinter\__init__.py", line 504, in callit
self.deletecommand(name)
File "C:\Python33\lib\tkinter\__init__.py", line 363, in deletecommand
self._tclCommands.remove(name)
AttributeError: 'NoneType' object has no attribute 'remove'
I really can't seem to find an answer. And to avoid solutions that I've already thought about: self.canvas is declared before the first run of the function, and is then only modified in the function above, so self.canvas is always a canvas object.
I'm also using threading, but don't know how this would affect the canvas object.
Edit:
The reason why I want to delete and create the canvas object between each frame is because i want the program to read the state of an underlying object and simply display it on the screen, and deleting the canvas object would be much easier than deleting every object on screen to redraw them.