tkinter tk wiget turtle python - python

i am trying to do a turtle game with the tkinter.Tk() class.
Here is the code:
from tkinter import *
import turtle
main = Tk(className="Castle Game")
s=turtle.TurtleScreen(cv=main,mode='standard', colormode=1.0, delay=10)
cover=turtle.RawTurtle(s)
It tries to return the following:
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "H:\2 Computer science\Year 9\4Python\Castle.py", line 7, in <module>
s=turtle.TurtleScreen(cv=main,mode='standard', colormode=1.0, delay=10)
File "C:\Python33\lib\turtle.py", line 989, in __init__
TurtleScreenBase.__init__(self, cv)
File "C:\Python33\lib\turtle.py", line 488, in __init__
self.cv.config(scrollregion = (-w//2, -h//2, w//2, h//2 ))
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 "-scrollregion"
>>>

In turtle graphics, the cv option stands for canvas, but you're giving it a Tk. That's why there's an error. To fix that, you will need to do this:
from tkinter import *
import turtle
root=Tk(className="Castle Game")
main = Canvas(root)
main.pack()
s=turtle.TurtleScreen(cv=main,mode='standard', colormode=1.0, delay=10)
cover=turtle.RawTurtle(s)
By changing main to Canvas and pack it onto a root.

Related

"wm_iconphoto" in tkinter multiple times

why i can't use wm_iconphoto two times?
root = Tk()
root_2 = Tk()
root.wm_iconphoto(#something)
root_2.wm_iconphoto(#same or another thing)
#Error
when i try set iconphoto for the second Tk window it returns this error:
Traceback (most recent call last):
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "c:\Users\lenovo\Desktop\clauth.py", line 41, in a91
b02.wm_iconphoto(False,b52)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 2183, in wm_iconphoto
self.tk.call('wm', 'iconphoto', self._w, *args)
_tkinter.TclError: can't use "pyimage1" as iconphoto: not a photo image

Can't add shape in Python turtle module - no such file or directory error

I'm trying to change the shape of the turtle to an 8bit link image. I have the image I want saved to the same directory as my source code (you can see that with os.getcwd().) I'm wondering why I am getting this error and how to fix it. Thanks!
from turtle import Turtle, Screen
import os
print(os.getcwd())
wn = Screen()
wn.bgcolor('lightblue')
spaceship = Turtle()
spaceship.color('red')
newshape = Screen().addshape('8bitlink.png.gif')
spaceship.shape(newshape)
spaceship.penup()
The error looks like this:
Traceback (most recent call last):
File "/Users/colind/Desktop/Spaceship Game.py", line 12, in <module>
newshape = Screen().addshape('8bitlink.png.gif')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 1133, in register_shape
shape = Shape("image", self._image(name))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 479, in _image
return TK.PhotoImage(file=filename)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 3542, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 3498, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "8bitlink.png.gif": no such file or directory
One issue is that you're not invoking addshape() correctly:
newshape = Screen().addshape('8bitlink.png.gif')
spaceship.shape(newshape)
As addshape() doesn't return anything, it registers the image shape under the name of the file, so you should instead do something like:
from turtle import Turtle, Screen
wn = Screen()
wn.bgcolor('lightblue')
wn.addshape('8bitlink.png.gif')
spaceship = Turtle()
spaceship.shape('8bitlink.png.gif')
spaceship.color('red')
spaceship.penup()
# ...
wn.mainloop()
This works for me when I store a GIF image under the name '8bitlink.png.gif'. You might convince yourself, and us, that you've an actual GIF image by running the Unix file command:
> file 8bitlink.png.gif
8bitlink.png.gif: GIF image data, version 89a, 24 x 24
>
You're output should be similar though not identical.

Python - 'bgpic' Not Working

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.

Python: Message error when using bgpic() from turtle module

I'm trying to change the background image in a turtle window.
The code reads simply as this:
import turtle
turtle.bgpic("france53.gif")
The turtle window shows up blank and I get the following error message:
Traceback (most recent call last):
File "<ipython-input-25-45373f8f3ea2>", line 1, in <module>
bgpic("france53.gif")
File "<string>", line 1, in bgpic
File "/Users/danielcharrier/anaconda/lib/python3.4/turtle.py", line 1482, in bgpic
self._setbgpic(self._bgpic, self._bgpics[picname])
File "/Users/danielcharrier/anaconda/lib/python3.4/turtle.py", line 738, in _setbgpic
self.cv.itemconfig(item, image=image)
File "<string>", line 1, in itemconfig
File "/Users/danielcharrier/anaconda/lib/python3.4/tkinter/__init__.py", line 2416, in itemconfigure
return self._configure(('itemconfigure', tagOrId), cnf, kw)
File "/Users/danielcharrier/anaconda/lib/python3.4/tkinter/__init__.py", line 1310, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
TclError: image "pyimage12" doesn't exist
The file "france53.gif" is in the working directory. What should I do to make it work?
I'm using the Anaconda distribution for Mac OS X.
Thanks!

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