有没有一种方法可以将tkinter元素与我要网格化的框架不同的类网格化

时间:2019-04-21 07:31:16

标签: python-3.x tkinter

我正在使用tkinter作为gui编写D&D模拟器,但是因为我想创建一个数组,其中每个索引对应于元素所在的行,因此对于Menu选项,我创建了自己的包含信息的类他们需要的选项列表和stringVar。但是,当我运行该程序时,即使已创建对象,它也不会将选项菜单网格化到框架上。

如果需要,请输入完整代码: https://pastebin.com/2FxBcUb2

运行的主类:

class dndSimulator(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.iconbitmap(self, default="Ampersand_on_Black (1).ICO")
        tk.Tk.wm_title(self, "D&D simulator")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartGame,MainMenu):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartGame)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

我的框架(它称为下拉框类的部分):

def addCharactersection(self, firstColumn):
        teamid = (firstColumn//4)
        self.removeDelete(firstColumn)

        self.placeholders = self.moveelements(firstColumn, 1, len(self.teamUiItems[teamid])-1)
        self.placeholders = self.placeholders[0]
        self.charactersection = []

        self.charactersection.append(dropdownMenu("",lstoffiles))
        self.charactersection.append(tk.Entry(self))
        self.charactersection.append(ttk.Button(self, text = "Commit", command = lambda: self.commitValues(firstColumn)))

        self.teamUiItems[teamid].insert(self.placeholders, self.charactersection)

        self.teamUiItems[teamid][self.placeholders][0].dropdowngrid(self.placeholders+2, 0,1)
        self.teamUiItems[teamid][self.placeholders][1].grid(row = self.placeholders+2,column = 1, columnspan = 1)
        self.teamUiItems[teamid][self.placeholders][2].grid(row = self.placeholders+2, column = 2, columnspan = 1)
        #grid these
        self.addDelete(firstColumn)
        print(self.teamUiItems[teamid][self.placeholders])

和下拉菜单类:

class dropdownMenu(tk.Frame):
    '''
    create a dropdown menu where this class includes all the vars and stuff
    '''

    def __init__(self,default,list):
        super(dropdownMenu, self).__init__()
        self.default = default
        self.list = list

        self.menuvar = tk.StringVar()
        self.menuvar.set(default)
        self.menu = tk.OptionMenu(self,self.menuvar, *self.list)

    def dropdowngrid(self,prow,pcolumn,pcolumnspan):
        self.menu.grid(row = prow, column = pcolumn, columnspan = pcolumnspan)

    def dropdownforget(self):
        self.menu.grid_forget()

    def dropdowninfo(self):
        return {'row':self.menu.grid_info()['row'], 'column':self.menu.grid_info()['column'], 'columnspan':self.menu.grid_info()['columnspan']}

我希望它可以网格化到显示在正确位置的框架上,但是即使留在frame类中的其余元素确实出现了,它也不会出现。

感谢您能提供帮助,因为过去一个小时左右我一直在尝试解决此问题!

1 个答案:

答案 0 :(得分:0)

编辑:测试代码后,我可以确认您忘记使用E/Unity: Unable to find arpresto_api E/Unity: DllNotFoundException: arcore_unity_api at (wrapper managed-to-native) GoogleARCoreInternal.ARPrestoCallbackManager+ExternApi.ArCoreUnity_setArPrestoInitialized(GoogleARCoreInternal.ARPrestoCallbackManager/EarlyUpdateCallback) at GoogleARCoreInternal.ARPrestoCallbackManager._Initialize () [0x0002c] in /Users/petrosmaliotis/New Unity Project 4/Assets/GoogleARCore/SDK/Scripts/Managers/ARPrestoCallbackManager.cs:181 at GoogleARCoreInternal.ARPrestoCallbackManager.get_Instance () [0x00018] in /Users/petrosmaliotis/New Unity Project 4/Assets/GoogleARCore/SDK/Scripts/Managers/ARPrestoCallbackManager.cs:91 at GoogleARCoreInternal.ARCoreAndroidLifecycleManager.get_Instance () [0x00023] in /Users/petrosmaliotis/New Unity Project 4/Assets/GoogleARCore/SDK/Scripts/Managers/ARCoreAndroidLifecycleManager.cs:80 at GoogleARCoreInternal.LifecycleManager.get_Instance () [0x00029] in /Users/petrosmaliotis/New Unity Project 4/Assets/GoogleARCore/SDK/Scripts/Managers/LifecycleManager.cs:43 at GoogleARCore.ARCoreS 来显示框架。 (首先我尝试使用.pack(),但是您的框架位于使用.grid()的窗口中)

您创建框架.pack()

dropdownMenu

您使用

 self.charactersection.append(dropdownMenu("",lstoffiles))

其中 self.teamUiItems[teamid][self.placeholders][0].dropdowngrid(self.placeholders+2, 0,1) 运行.grid()

OptionMenu

但是选项菜单def dropdowngrid(self, ...): self.menu.grid(...) 位于框架self.menu内,而您忘记了self

添加后,我可以在窗口中看到它。

self.pack()

enter image description here