簡體   English   中英

Tkinter Canvas create_window function 沒有顯示所需的小部件

[英]Tkinter Canvas create_window function does not show the desired widgets

我試圖為我的應用程序制作一個自定義容器 function,但是當我傳遞小部件列表時,它們不可見。 容器類似於LabelFrame ,但帶有圓角。 我嘗試了很多東西,但似乎沒有任何效果。 請讓我知道我做錯了什么。

這是我的代碼和一些評論:

parent -> 這是頂層 window

lbl_text -> 這將是LabelFrame頂部的小文本

widgets -> 這是我想放置在圓形框架中的小部件列表

def rndframe(parent, lbl_text, widgets
    # content_positioner and max_width is for calculate the requiered size of the canvas to be big 
    # enough for all the passed widgets, and to determine the coordinates for create_window function. 
    # It works perfectly and I get all the information I want.
    content_positioner = [14]
    max_width = 1
    for i, item in enumerate(widgets):
        content_positioner.append(content_positioner[i]+widgets[i].winfo_reqheight())
        if widgets[i].winfo_reqwidth() > max_width:
            max_width = widgets[i].winfo_reqwidth()

    height = max(content_positioner)+10
    width = max_width+10

    # Here I create the canvas which will contain everything
    canv = TK.Canvas(parent, height=height, width=width, bg='green', bd=0, highlightthickness=0)
    
    # Here I draw the rounded frame
    radius = 10
    x1 = 2
    y1 = 5
    x2 = width-2
    y2 = height-2
    points = [x1+radius, y1, x2-radius, y1, x2, y1, x2, y1+radius, x2, y2-radius, x2, y2,               
              x2-radius, y2, x1+radius, y2, x1, y2, x1, y2-radius, x1, y1+radius, x1, y1]             
    canv.create_polygon(points, smooth=True, outline=DS.menu_bg, width=3, fill='')

    # Here I put the litle label in the frmae 
    label = TK.Label(parent, text=lbl_text, bg=DS.main_bg, padx=3, pady=0, fg=DS.main_textcolor)
    canv.create_window(18, 5, anchor='w', window=label)

    # And thats the part where I would like to place all the passed widgets based on the coordinate 
    # list I preveously created but nothing appear just the frame polygon and the liitle label on it.
    for w, widget in enumerate(widgets):
        canv.create_window(width/2, content_positioner[w], anchor='n', window=widgets[w])

    return canv

最后是我嘗試使用的方式:

id_num = TK.Label(result_area_leaf, text='123-4567')
id_num2 = TK.Label(result_area_leaf, text='123-4567')
id_holder = rndframe(result_area_leaf, lbl_text='id', widgets=[id_num, id_num2])
id_holder.grid()

您的代碼有很多問題,但問題的根源是您添加到 canvas 的小部件的堆疊順序低於 canvas,因此它們位於 canvas 的后面或“下方”。這是因為默認堆疊順序最初由小部件的創建順序決定。 您在 canvas 之前創建小部件,因此它們的堆疊順序較低。

堆疊順序問題的最簡單解決方案是將小部件提升到 canvas 以上,您可以使用lift方法來完成。

for w, widget in enumerate(widgets):
    canv.create_window(width/2, content_positioner[w], anchor='n', window=widgets[w])
    widgets[w].lift()

當我進行上述修改並修復代碼中的所有其他問題時,標簽出現在 canvas 中。

最后我得到它的工作。 首先很抱歉我沒有首先發布獨立的可運行代碼。 當我將代碼放入文件中並對缺失的變量進行必要的更改時,建議的.lift()方法起作用了。 原因是在那個單個文件中,我使用根目錄 window 作為父目錄。

在我復制的代碼中,兩個標簽( id_numid_num2 )有一個 Canvas 用於父級(result_area_leaf),這就是問題所在。 如果我將我的根 window 作為傳遞的小部件的父級,則會顯示這些小部件。 .lift()實際上根本不需要。

謝謝你們;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM