未显示框架上的画布

时间:2017-03-09 13:01:45

标签: python tkinter

我在tkinter中的Canvas内有一个Frame。框架也有背景颜色和画布。但看似框架背景覆盖了画布颜色。

如何增加画面背景的透明度,使画布可见?

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()

所以,基本上,当您在框中输入一个数字并按下Enter时,会在红色部分(框架)中创建一个画布,并且应该在该部分上绘制网格图案帆布。基本上,有2个框架,顶部框架包含按钮和输入框,下部框架应该能够在画布内创建内容。

1 个答案:

答案 0 :(得分:2)

未显示画布的原因是因为您没有告诉它在frame1内显示,即您忘记pack(或grid或{{1它,所以只是在此期间做:

place

现在,从布局的角度来看,您可能需要更好地考虑如何使用... mainCanvas=Tkinter.Canvas(frame1, bg="white", height=1000, width=1000) mainCanvas.pack() ... packgrid

以上是上述修正后的结果(在Mac OS X,Sierra上)

点击pack

之前

enter image description here

点击Enter

enter image description here

一般来说,只要记住如果一个框架不包含任何具有特定指定大小的窗口小部件,那么它将具有一个空体。