+ 1

How to set a canvas into the main window?

I was trying to put a Canvas into the main window. But although the master is the main window and I pack it like every other widget, it doesn't work as expected: A second Tk opens, which is the Canvas, separate from the root. Why does this happen and is it somehow possible to do what I want instead?

18th Feb 2019, 8:51 PM
HonFu
HonFu - avatar
16 Answers
+ 1
HonFu I’m pretty sure that canvas should work like every other widget so it shouldn’t open a new window. Copy this code and run it: from tkinter import * master = Tk() canvas_width = 80 canvas_height = 40 w = Canvas(master, width=canvas_width, height=canvas_height) w.pack() y = int(canvas_height / 2) w.create_line(0, y, canvas_width, y, fill="#476042") mainloop() What happens when you run it?
19th Feb 2019, 11:00 PM
Juho Pesonen
Juho Pesonen - avatar
+ 1
I'm just starting out with tkinter. My understanding was that there is a 'mommy', that Tk-object, and that every widget declaring her master will be part of that mommy window. And that is true for Lable, Button, RadioButton, Entry, Checkbutton, Frame, Scale... Only Canvas is being a spoilsport starting a thing of her own.
19th Feb 2019, 10:49 PM
HonFu
HonFu - avatar
+ 1
HonFu (Incase you’re still having problems with the program) I just ran your code on my pc and it worked like I expected. So it opened a new window and packed the canvas on there and it didn’t open any new windows.
20th Feb 2019, 8:35 AM
Juho Pesonen
Juho Pesonen - avatar
+ 1
Hm, interesting... I am not close to my PC right now, but I will copypaste this into there later and report.
20th Feb 2019, 8:37 AM
HonFu
HonFu - avatar
+ 1
HonFu No problem :)
20th Feb 2019, 8:54 PM
Juho Pesonen
Juho Pesonen - avatar
0
Can you share your code, please?
19th Feb 2019, 10:09 PM
Juho Pesonen
Juho Pesonen - avatar
0
I appreciate the spirit of letting people show code; in this case I'll just be spelling out what I already wrote though. from tkinter import * m = Tk() c = Canvas(master=m) c.pack() m.mainloop() What I expect: A canvas as one element of the main window. What I get: Two separate windows. Any way around this?
19th Feb 2019, 10:23 PM
HonFu
HonFu - avatar
0
HonFu Maybe the m in m.mainloop() calls Tk() again and it opens a new window
19th Feb 2019, 10:30 PM
Juho Pesonen
Juho Pesonen - avatar
0
Goodester, have you worked with tkinter?
19th Feb 2019, 10:34 PM
HonFu
HonFu - avatar
0
HonFu Yep, a little bit.
19th Feb 2019, 10:35 PM
Juho Pesonen
Juho Pesonen - avatar
0
HonFu I don’t see anything wrong with your code though and I can’t really debug it right now because I can’t use my pc.
19th Feb 2019, 10:40 PM
Juho Pesonen
Juho Pesonen - avatar
0
Have you used Canvas yourself?
19th Feb 2019, 10:40 PM
HonFu
HonFu - avatar
0
HonFu Yes like a year ago I don’t use it very often
19th Feb 2019, 10:42 PM
Juho Pesonen
Juho Pesonen - avatar
0
HonFu Have you tried to pack some other widget instead of Canvas on the master so you could see if Canvas is causing the bug?
19th Feb 2019, 10:48 PM
Juho Pesonen
Juho Pesonen - avatar
0
It works perfectly fine with everything else.
19th Feb 2019, 10:51 PM
HonFu
HonFu - avatar
0
Goodester, I just tried your example... ... and it worked. oO Now I am relieved that everything is as it should be, but the question, what the heck I was doing differently, will be haunting me for a while... Anyway, thanks a lot for your help! :)
20th Feb 2019, 8:50 PM
HonFu
HonFu - avatar