user2277550
user2277550

Reputation: 621

Canvas on a frame not being displayed

I have a Canvas inside a Frame in tkinter. The frame has a background color and the canvas too. But seemingly the frame background overrides the canvas color.

How can I increase the transparency of the frame background such that the canvas is visible?

import Tkinter
import tkMessageBox
from Tkinter import *


top = Tkinter.Tk()

frame = Frame(top, width=1000, height=1000, background="bisque")
frame.pack()

bottomframe = Frame(top, width=1000, height=1000, background="red")
bottomframe.pack( side = BOTTOM )


def creatLayers(no_of_layers, max_nodes_in_each_layer, frame1=bottomframe):
    print 'here2'

    listLayerRect=[]
    listDelimiterRect=[]

    #The canvas is created here.
    mainCanvas=Tkinter.Canvas(frame1, bg="white", height=1000, width=1000)
    frame1.pack(side=LEFT)

    for i in range (0,no_of_layers):            
        print 'here3'

        x=15*i

        #rectangles that are being drawn on the canvas.
        mainCanvas.create_polygon(x,0,x+10,0,x+10,1000,x,1000, outline='gray', fill='gray', width=2)

#        listLayerRect.append(Tkinter.Canvas(frame1, bg="blue", height=1000, width=30))
#        listDelimiterRect.append(Tkinter.Canvas(frame1, bg="yellow", height=1000, width=30))


L1 = Label(frame, text="Layers")
E1 = Entry(frame, bd =8)
L2 = Label(frame, text="Layers2")

def helloCallBack(E=E1,):
   # tkMessageBox.showinfo( "Hello Python", "Hello World")
   k=int(E.get())
   print 'here'
   print k

   creatLayers(k,k)      

B = Tkinter.Button(frame, text ="Enter", command = helloCallBack)

B.pack(side=LEFT)
#L1.pack(side=LEFT)
E1.pack(side=LEFT)
#L2.pack(side=LEFT)

top.mainloop()

So, basically, when you enter a number in the box and press Enter, a canvas gets created in the red part (frame) and a grid pattern should be drawn on that canvas. Essentially, there are 2 frames, the top frame contains the button and the entry box, the lower frame should be able to draw stuff inside on the canvas created within.

Upvotes: 3

Views: 7921

Answers (1)

nbro
nbro

Reputation: 15837

The reason why the canvas is not displayed is because you're not telling it to be displayed inside frame1, i.e. you forgot to pack (or grid, or place) it, so just do in the meantime:

...
mainCanvas=Tkinter.Canvas(frame1, bg="white", height=1000, width=1000)
mainCanvas.pack()
...

Now depending on what you really want to achieve from the layout point of view, you may need to think better how to use pack, grid and pack.

Here's the result after the correction above (on Mac OS X, Sierra)

Before clicking Enter

enter image description here

After clicking Enter

enter image description here

In general, just remember that a frame will have a empty body if it doesn't contain any widget with a certain specified size.

Upvotes: 4

Related Questions