简体   繁体   English

自定义Tkinter窗口小部件不会在顶层打包吗?

[英]Custom Tkinter Widget Won't Pack in Toplevel?

I'm trying to make a custom Tkinter widget by subclassing a Frame to make a scrollable widget that contains items. 我正在尝试通过将Frame子类化以制作包含项目的可滚动小部件来制作自定义Tkinter小部件。 I have 2 windows, my root window and a toplevel window and I need my custom widget to pack to the toplevel window. 我有2个窗口,我的根窗口和一个顶层窗口,我需要自定义窗口小部件打包到顶层窗口。

My problem is, that despite making 'top' the parent of my custom widget, it still packs in the root window. 我的问题是,尽管将“ top”设为自定义小部件的父级,但它仍打包在根窗口中。 I've tried other widgets and they pack to the toplevel window fine. 我试过其他小部件,它们可以打包到顶级窗口中。

my code: 我的代码:

from tkinter import *

root = Tk()
root.config(bg="#000000")
root.wm_attributes("-alpha","0.7")

top = Toplevel()
top.config(bg="#000001")
top.wm_attributes("-topmost",1)
top.wm_attributes("-transparentcolor","#000001")
top.wm_title("TOPLEVEL")



class Scrollygrid(Frame):
    def __init__(self, parent, columns, h, w):
        super(Scrollygrid, self).__init__()
        self.scrollbar = Scrollbar(self)
        self.scrollbar.pack(side = RIGHT, fill = Y)
        self.area = Canvas(self, yscrollcommand=self.scrollbar.set, width = w, height = h, bg = "#000001", bd = 0, highlightthickness = 0)
        self.gridframe = Frame(height = h*10, width = w, bg = "#FF0000")
        self.gridframe.pack_propagate(0)
        self.area.create_window((0, 0), window = self.gridframe)
        for i in range(500):
            Label(self.gridframe, text = i).pack()
        self.area.pack()
        self.area.config(scrollregion = (self.area.bbox("all")))
        self.scrollbar.config(command = self.area.yview)
        def onScroll(event):
            self.area.yview_scroll(int(-1*(event.delta/60)), "units")
        self.area.bind_all("<MouseWheel>", onScroll)
        self.scrollbar.pack_forget() #scroll wheel still works!

testgrid = Scrollygrid(top, 1,root.winfo_screenheight()-80,root.winfo_screenwidth()-(root.winfo_screenwidth()/10))
testgrid.pack(side = RIGHT, anchor = CENTER)

root.mainloop()

the transparent color and alpha level have been left in to make it more immediately obvious which window is which. 保留了透明的颜色和Alpha级别,以使您立即更清楚地看到哪个窗口。

top isn't the parent of your custom widget. top不是您的自定义小部件的父级。 You're never passing it. 你永远不会过去。 Your Scrollygrid is being passed a parent parameter and that's it . 您的Scrollygrid正在传递parent参数,仅此而已 Nothing tells it to assign it as the parent as an attribute even. 没有任何东西告诉它甚至将其分配为父项作为属性。 So the parent defaults to the Tk instance, root . 因此,父级默认Tk实例root

Replace: 更换:

super(Scrollygrid, self).__init__()

with: 与:

super(Scrollygrid, self).__init__(parent)

In order to pass the given widget argument as the parent to the superclass, Frame . 为了通过给定的插件参数作为超类, Frame Essentially making the Toplevel , top your custom class' parent. 从根本上让Topleveltop自定义类的父。 Upon which you'll get further error(s) but the parent is correctly assigned. 在这之后,您将得到进一步的错误,但已正确分配了父级。 Verify by: 验证方式:

print(repr(self.winfo_toplevel()))

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

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