Home > Back-end >  One of my canvases doesn't show in the Tkinter window (Python)
One of my canvases doesn't show in the Tkinter window (Python)

Time:09-26

I'm trying to make a Tkinter window with two canvases: one for inputs, one for results.

The reason for that is that I couldn't find a way to clear just the results when the next input comes in so if there's a way to do that, that would also solve my problem, but I didn't find a way to solve that.

The issue is that the second canvas won't show up on the window. I've tried putting something on it right away, but it still didn't work. Also when searching for a solution, I found a code that I tried for myself and then it worked.

Here's the code:

root = tk.Tk() #I used import tkinter as tk

canvas = tk.Canvas(root, width = 400, height = 200)
canvas.pack(side='top', anchor='nw', fill='x')

canvas2 = tk.Canvas(root, width = 400, height = 600)
canvas.pack(side='top', anchor='nw', fill='both')

CodePudding user response:

You typed canvas.pack(side='top', anchor='nw', fill='both') instead of canvas2.pack(side='top', anchor='nw', fill='both'). Here is the corrected code:

import tkinter as tk
root = tk.Tk()

canvas = tk.Canvas(root, width=400, height=200, bg="red")
canvas.pack(side='top', anchor='nw', fill='x')

canvas2 = tk.Canvas(root, width=400, height=600, bg="blue")
canvas2.pack(side='top', anchor='nw', fill='both')

root.mainloop()
  • Related