I'm very new to Python. I wanted to display a turtle object on top of a canvas in Tkinter. I'm not sure why the object is not displaying.
I used the RawTurtle() in order to use the Tkinter canvas as screen.
root =tk.Tk()
outercanvas = Canvas(root, width=900, height=800, bg='#00ffff')
outercanvas.pack(expand=Y,fill=BOTH)
innercanvas = Canvas(outercanvas, width=680, height=700)
outercanvas.create_window(100, 40, anchor=NW, window=innercanvas)
bg = tk.PhotoImage(file="level2.png")
innercanvas.create_image(-5, 0, image = bg, anchor=NW)
bob = turtle.RawTurtle(innercanvas)
I expect the turtle to appear on top of the canvas, so that I can manipulate it later.
What am I missing?
Your background image and the turtle appear to be in conflict. Try it this way instead:
import tkinter as tk
from turtle import RawTurtle, TurtleScreen
root = tk.Tk()
outercanvas = tk.Canvas(root, width=900, height=800, bg='#00ffff')
outercanvas.pack(expand=tk.Y, fill=tk.BOTH)
innercanvas = tk.Canvas(outercanvas, width=680, height=700)
outercanvas.create_window(100, 40, anchor=tk.NW, window=innercanvas)
screen = TurtleScreen(innercanvas)
screen.bgpic("level2.gif")
bob = RawTurtle(screen)
bob.circle(100)
screen.mainloop()
Note that I had to make and use "level2.gif" for this to work but you may have a newer underlying tkinter, and turtle, that accepts PNG files.
Related
I want to have my digital clock on top of the background. Currently the time is displaying above the image that im using
How should i set up this code? This is what i have come up with so far
from tkinter import *
from PIL import ImageTk,Image
import time
root=Tk()
root.title("Klocka")
root.attributes("-topmost", 1)
root.iconbitmap('klocka.ico (1).ico')
root.geometry('300x200+50+50')
canvas=Canvas(root,width=300,height=140)
image=ImageTk.PhotoImage(Image.open("C:\\Users\\frass\\Desktop\\python projekt\\strand123.png"))
canvas.create_image(0,0,anchor=NW,image=image)
canvas.pack()
def present_time():
display_time = time.strftime("%I:%M:%S")
digi_clock.config(text=display_time)
digi_clock.after(200,present_time)
digi_clock = Label(root, font=("Times New Roman",50),activebackground="white",fg="black")
digi_clock.pack()
present_time()
root.mainloop()
You can use place() instead of pack() to place the clock on top of the center of the canvas:
digi_clock.place(x=150, y=70, anchor="c")
However the background of the clock label is not transparent.
You can use canvas.create_text() instead to show the clock and use canvas.itemconfigure() to update the clock:
...
def present_time():
display_time = time.strftime("%I:%M:%S")
# update the clock text
canvas.itemconfigure("clock", text=display_time)
root.after(200, present_time)
# clock text
canvas.create_text(150, 70, font=("Times New Roman",50), anchor="c", tags="clock")
present_time()
root.mainloop()
hope you're all doing well. I've got a problem I hope you can help with.
I'm trying to build a board game in tkinter. This will have different shaped tiles being placed in squares on top of a background image. I have managed to add in the background with tk.PhotoImage and tk.Label, and correctly resized the image of the tile with ImageTk.PhotoImage.
However, when I place the tile on the board, all transparency is lost and replaced with monotone grey.
Minimal Code:
from PIL import Image,ImageTk
import tkinter as tk
def tile_push():
pass
# Create background image
root = tk.Tk()
root.geometry("390x500") # Size of background board
background_image = tk.PhotoImage(file="Gameboard.png")
background_label = tk.Label(root, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
# Create button
im = Image.open("Chick.png").resize((100,100))
image_player_1 = ImageTk.PhotoImage(im)
b = tk.Button(root, image=image_player_1, command=tile_push, borderwidth=0, highlightthickness=0)
b.place(x=140, y=258, width=115, height=115)
tk.mainloop()
A similar question on SO shows how to set the background as black, but I need the background to be transparent.
Any help will be greatly appreciated!
Thanks for the support acw, I've managed to get it going now. This is what I did:
from PIL import Image,ImageTk
import tkinter as tk
def tile_push(*args, **kwargs):
print("It's alive!")
# Create background image
root = tk.Tk()
root.geometry("390x500") # Size of background board
canvas = tk.Canvas(root, width=390, height=500)
canvas.place(x=0,y=0)
background_image = tk.PhotoImage(file="Gameboard.png")
canvas.create_image((0,0), anchor="nw", image=background_image)
# Create button
im = Image.open("Chick.png").resize((115,115))
imTk = ImageTk.PhotoImage(im)
chick = canvas.create_image((140,258), anchor="nw", image=imTk)
canvas.tag_bind(chick, '<Button-1>', tile_push)
tk.mainloop()
I'm using Tkinter combined with Python Turtle Graphics and I want to be able to get the width of a button made using create_window().
I am able to set the value using the code below, but I want to get the value rather than set it. How do I do this please?
import turtle
import tkinter as tk
screen = turtle.Screen()
canvas = screen.getcanvas()
button = tk.Button(canvas.master, text="Press me")
w1 = canvas.create_window(0, 0, window=button)
canvas.itemconfig(w1, width=100)
turtle.done()
This will do it.
import turtle
import tkinter as tk
screen = turtle.Screen()
canvas = screen.getcanvas()
button = tk.Button(canvas.master, text="Press me")
w1 = canvas.create_window(0, 0, window=button)
canvas.itemconfig(w1, width=100)
button.update()
print(button.winfo_width())
print(button.winfo_height())
turtle.done()
Make sure to update before calling .winfo_width(). :-)
I'm trying to make the turtle move faster, which I would normally do using
import turtle as t
t.speed(0)
t.tracer(0,0)
But when I have it in a canvas using RawTurtle(), I'm not sure how to do this.
root = tk.Tk() #create root window
#create turtle canvas
canvas = tk.Canvas(root,width=500,height=500)
canvas.pack()
t = turtle.RawTurtle(canvas)
t.ht()
Here's my code. Anyone know how?
First, either do one or the other of these, not both:
t.speed(0)
t.tracer(0,0)
The speed(0) aka speed('fastest') gets you the fastest drawing animation. The tracer(0) eliminates drawing animation altogether. They don't add together.
You can get maximum speed, i.e. eliminate drawing animation, in turtle embedded in a Canvas by using turtle's TurtleScreen() wrapper:
import tkinter as tk
from turtle import TurtleScreen, RawTurtle
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
canvas.pack()
screen = TurtleScreen(canvas)
screen.tracer(False)
turtle = RawTurtle(screen)
turtle.circle(100)
screen.tracer(True)
screen.mainloop()
I made a few edits to your code. For starters, you need to work all the commands after the t = turtle.RawTurtle(canvas) line. Then you need to add a .mainloop() function at the end.
This would be your final code:
import tkinter as tk
root = tk.Tk() #create root window
import turtle
#create turtle canvas
canvas = tk.Canvas(root,width=500,height=500)
canvas.pack()
t = turtle.RawTurtle(canvas)
t.speed(1)
t.forward(100)
t.ht()
root.mainloop()
When the speed is set to 1, this is the output:
When the speed is set to 0, this is the output:
Sorry about the gifs, and hope this helps!
I am trying to create a really simple HMI, to display different images and name of the file and other information in a lower bar using tkinter. I have created a layout, added the background image but the lower rectangle is not visible.
The background image overlays the rectangle, how can I force the ground image to behind all the other components inside the canvas? The black rectangle should be visible at the lower section of the picture
from PIL import Image, ImageTk
from tkinter import Tk, BOTH, Canvas
from tkinter.ttk import Frame, Label, Style
root = Tk()
root.title('Screen')
w = Canvas(root, width=800 , height=480)
w.pack()
back_ground = ImageTk.PhotoImage(Image.open("./icon/sync_background.bmp"))
back_ground_label = Label(image=back_ground, borderwidth=0)
back_ground_label.place(x=0,y=0)
w.create_rectangle(0, 400, 800, 480,outline="#000", fill="#000")
w.pack()
root.mainloop()
Your background image is placed on top of the canvas and so it hides the canvas. You should create the image using w.create_image(...) instead:
from PIL import Image, ImageTk
from tkinter import Tk, BOTH, Canvas
root = Tk()
root.title('Screen')
back_ground = ImageTk.PhotoImage(Image.open("./icon/sync_background.bmp"))
w = Canvas(root, width=800 , height=480)
w.pack()
w.create_image(0, 0, image=back_ground, anchor='nw')
w.create_rectangle(0, 400, 800, 480,outline="#000", fill="#000")
root.mainloop()
Here is what you can do:
from PIL import Image, ImageTk
from tkinter import Tk, BOTH, Canvas
from tkinter.ttk import Frame, Label, Style
root = Tk()
root.title('Screen')
w = Canvas(root, width=800 , height=480)
w.pack()
back_ground = ImageTk.PhotoImage(Image.open("./icon/sync_background.bmp"))
back_ground_label = Label(image=back_ground, borderwidth=0)
back_ground_label.place(x=0,y=0)
rect = w.create_rectangle(0, 400, 800, 480,outline="#000", fill="#000")
w.tag_raise(rect)
w.pack()
root.mainloop()
What this does is creates an object "rect" and then uses the tag_raise method on it to bring it in front.
Hope it helps!
Cheers!