How to clear Tkinter Canvas?
Every canvas item is an object that Tkinter keeps track of. If you are clearing the screen by just drawing a black rectangle, then you effectively have created a memory leak -- eventually your program will crash due to the millions of items that have been drawn.
To clear a canvas, use the delete method. Give it the special parameter "all"
to delete all items on the canvas (the string "all"
" is a special tag that represents all items on the canvas):
canvas.delete("all")
If you want to delete only certain items on the canvas (such as foreground objects, while leaving the background objects on the display) you can assign tags to each item. Then, instead of "all"
, you could supply the name of a tag.
If you're creating a game, you probably don't need to delete and recreate items. For example, if you have an object that is moving across the screen, you can use the move or coords method to move the item.
How to delete the canvas from Tkinter after shown the window
Don't need to delete, you can update all of lines:
import tkinter as tk
import random
import numpy as np
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.frame = tk.Frame(self, background="bisque")
self.frame.pack(side="top", fill="both", expand=True)
self.canvas = tk.Canvas(self.frame)
self.line = self.canvas.create_line(100, 100, 300, 200, fill="blue")
self.canvas.pack(side="top", fill="both", expand=True)
self.draw_line()
def remove_line(self, line):
self.canvas.delete(line)
def get_h(self):
h1 = [-1, -0.86, -0.71, -0.57, -0.43, -0.29, -0.14, 0, 0.14, 0.29,0.43, 0.57, 0.71, 0.86, 1]
map_h = np.linspace(100, 300, 15)
map_h = np.around(map_h, 3)
map_h = map_h.tolist()
rnd_h1 = map_h[h1.index(random.choice(h1))]
rnd_h2 = map_h[h1.index(random.choice(h1))]
return rnd_h1, rnd_h2
def draw_line(self):
rnd_h1, rnd_h2 = self.get_h()
self.canvas.coords(self.line, 100, rnd_h1, 300, rnd_h2)
self.after(1000, self.draw_line)
app = App()
app.mainloop()
Python Overwriting text in Tkinter
To cancel timer. Save return value of after
, call after_cancel
with saved value.
from Tkinter import *
remain = 11
_timer = None
def cdtimerr():
global remain, _timer
remain -= 1
cdtext = canvas.create_text(510, 6, text=remain, font="Ubuntu 29 bold", anchor=NW)
if remain == 0:
canvas.delete(ALL)
else:
_timer = canvas.after(1000, lambda: (canvas.delete(cdtext), cdtimerr()))
root = Tk()
root.geometry('1024x768')
canvas = Canvas(root)
canvas.pack(expand=1, fill=BOTH)
_timer = canvas.after(0, cdtimerr)
Button(root, text='Cancel', command=lambda: canvas.after_cancel(_timer)).pack()
root.mainloop()
Replace Canvas Image in Tkinter
Instead of using Canvas, I replaced the code so that it prints the image using tkinter.Label:
from PIL import ImageTk,Image, ImageFont, ImageDraw
import tkinter
import textwrap
from tkinter import Frame, Canvas, Text, INSERT, END
root = tkinter.Tk()
root.geometry("296x337")
root.resizable(False, False)
img = ImageTk.PhotoImage(Image.open("red.jpg"))
panel = tkinter.Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
def changepic(imagename):
img2 = ImageTk.PhotoImage(Image.open(imagename))
panel.configure(image=img2)
panel.image = img2
a2=tkinter.Button(root,text='change color',bd=0, command=changepic("blue.jpg")
a2.config(highlightbackground='black')
a2.place(x=135, y=70)
I got my information from: How to update the image of a Tkinter Label widget?
And: https://www.tutorialspoint.com/python/tk_label.htm
Related Topics
How to Pass Variables from Python Script to Bash Script
Pyspark: How to Duplicate a Row N Time in Dataframe
Move Files Between Two Aws S3 Buckets Using Boto3
Vscode Import Error for Python Module
Convert Images from [-1; 1] to [0; 255]
How to Download the Latest File of an S3 Bucket Using Boto3
Creating New Dataframes in Loop in Python
How to Execute Local Python Scripts in Jenkins Ui
How to Merge 2 CSV Files Together by Multiple Columns in Python
How to Extract X,Y Coordinates from Opencv "Cv2.Keypoint" Object
How to Get All Possible Combinations of a List'S Elements
Cmake on Linux Centos 7, How to Force the System to Use Cmake3
Python Db-Api: Fetchone VS Fetchmany VS Fetchall
How to Make a Function Change Variables While in a While Loop
Parentheses and Quotation Marks in Output
How to Select All Elements Greater Than a Given Values in a Dataframe