简体   繁体   English

无法使create_window在Canvas上工作。 Tkinter Python 3.6

[英]Can't get create_window to work on a Canvas. Tkinter Python 3.6

I have made my own widget called 'InventorySlot' and I need the widgets within my custom one to be made onto a Canvas instead of using 'grid' or 'pack'. 我已经制作了一个名为“ InventorySlot”的小部件,我需要将自定义部件中的这些小部件制作到Canvas上,而不是使用“ grid”或“ pack”。

import tkinter as tk
from tkinter import *
from tkinter.ttk import *

class Main:
    def __init__(self):
        self.root = Tk()
        self.root.geometry('500x500')
        self.test = InventorySlot(self.root)
        self.test.grid()

class InventorySlot(tk.Frame):
    def __init__(self,parent,*args,**kwargs):
        tk.Frame.__init__(self,parent)
        self.options = {}
        self.options.update(kwargs)
        self.slot = tk.Label(self,height=3,width=6,text='',relief=SUNKEN,bg='#8b8b8b',bd=4,padx=1,pady=0)
        self.canvas = Canvas(self)
        self.canvas.create_window(10,10,window=self.slot)
        self.canvas.grid()

MainTk = Main()



MainTk.root.mainloop()

All it shows is a blank Canvas 它显示的只是一块空白的画布

You need to create the label after creating the canvas. 创建画布后,需要创建标签。 The order of creation determines the stacking order (ie: the z-index). 创建顺序确定堆叠顺序 (即z-index)。 The label on s there, it's just behind the canvas. s上的标签在画布后面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM