How to use 'how to add image in tkinter window' in Python

Every line of 'how to add image in tkinter window' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
284def _show(image, title):
285 """Helper for the Image.show method."""
286
287 class UI(tkinter.Label):
288 def __init__(self, master, im):
289 if im.mode == "1":
290 self.image = BitmapImage(im, foreground="white", master=master)
291 else:
292 self.image = PhotoImage(im, master=master)
293 super().__init__(master, image=self.image, bg="black", bd=0)
294
295 if not tkinter._default_root:
296 raise OSError("tkinter not initialized")
297 top = tkinter.Toplevel()
298 if title:
299 top.title(title)
300 UI(top, image).pack()
46def __init__(self, root):
47 super().__init__(root)
48 self.circle = 0
49 self.drawn_img = None
50 self.screen_width = root.winfo_screenwidth()
51 self.screen_height = root.winfo_screenheight()
52 self.start_x, self.start_y = 0, 0
53 self.end_x, self.end_y = 0, 0
54 self.current_item = None
55 self.fill = "#00ff00"
56 self.fill_pil = (0,255,0,255)
57 self.outline = "#00ff00"
58 self.brush_width = 2
59 self.background = 'white'
60 self.foreground = "#00ff00"
61 self.file_name = "Untitled"
62 self.tool_bar_functions = (
63 "draw_line", "draw_irregular_line"
64 )
65 self.selected_tool_bar_function = self.tool_bar_functions[0]
66
67 self.create_gui()
68 self.bind_mouse()
69
70 # Create blank image to avoid errors with irregular line drawing on blank canvas
71 self.canvas.img = Image.new('RGB', (800,1280), (255, 255, 255))
72 self.canvas.img_width, self.canvas.img_height = self.canvas.img.size
73 # make reference to image to prevent garbage collection
74 # https://stackoverflow.com/questions/20061396/image-display-on-tkinter-canvas-not-working
75 self.canvas.tk_img = ImageTk.PhotoImage(self.canvas.img)
76 self.canvas.config(width=self.canvas.img_width, height=self.canvas.img_height)
77 self.canvas.create_image(self.canvas.img_width / 2.0, self.canvas.img_height / 2.0, image=self.canvas.tk_img)
78
79 self.drawn_img = Image.new("RGBA", self.canvas.img.size)
80 self.drawn_img_draw = ImageDraw.Draw(self.drawn_img)

Related snippets