不能将 tkinter 小部件放在顶层框架内 [英] Can't put tkinter widget inside Toplevel Frame

查看:27
本文介绍了不能将 tkinter 小部件放在顶层框架内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过按下按钮打开一个顶级小部件,并根据我找到的一个示例在该框架内生成一个列表.但是,当我尝试将小部件放置在生成的框架内时,出现以下错误:

I am trying to open a toplevel widget from a button press, and generate a list within that frame from an example I found. However, when I try to place the widget within the generated frame, I get the following error:

_tkinter.TclError: can't put .!treeview inside .!errorexample.!toplevel.!mclistdemo.!frame.!frame

我已将问题缩小到

self.tree.grid(in_=f, row=0, column=0,sticky=NSEW)

self.tree.grid(in_=f, row=0, column=0, sticky=NSEW)

在 _create_treeview 方法中.删除 in_ 命令后,小部件在父窗口中正确生成.我怀疑这个问题与我的父/自命名约定有关,但我仍在努力掌握这个主题.

within the _create_treeview method. When the in_ command is removed, the widget is generated correctly in the parent window. I suspect that the problem has something to do with my parent/self naming conventions, but I am still struggling to grasp that subject.

我遇到的大多数类似描述的问题似乎都是在生成小部件时尝试放置小部件的问题,但在此代码中似乎并非如此.

Most of the questions I've run across that are described similarly seem to be a matter of trying to place the widget while generating it, but that doesn't seem to be the case in this code.

from tkinter import *
from tkinter import ttk
from tkinter.font import Font

class ErrorExample(Frame):

    def __init__(self, parent):
        Frame.__init__(self,parent)
        self.grid()
        self.parent=parent

        self.b4=Button(
            self,
            text="Load",
            command=lambda: self.createWindow())
        self.b4.grid()

    def createWindow(self):
        self.t = Toplevel(self)
        MCListDemo(self)

class MCListDemo(ttk.Frame):

    def __init__(self, parent, isapp=True):
        ttk.Frame.__init__(self, parent.t)
        self.grid()
        self.isapp = isapp
        self._create_widgets()

    def _create_widgets(self):
        if self.isapp:
            self._create_demo_panel()

    def _create_demo_panel(self):
        demoPanel = Frame(self)
        demoPanel.grid()
        self._create_treeview(demoPanel)
        self._load_data()

    def _create_treeview(self, parent):
        f = ttk.Frame(parent)
        f.grid()
        self.dataCols = ('country', 'capital', 'currency')        
        self.tree = ttk.Treeview(columns=self.dataCols, 
                                 show = 'headings')
        self.tree.grid(in_=f, row=0, column=0, sticky=NSEW)

start=Tk()
ErrorExample(start)
if __name__=="__main__":
    main()

推荐答案

你没有给树视图一个父级,所以它有一个作为父级的根窗口.小部件存在于层次结构中,并且小部件不能放置在层次结构的不同部分.

You don't give the treeview a parent, so it has the root window as a parent. Widgets live in a hierarchy, and widget's can't be placed in a different part of the hierarchy.

官方文档是这样描述的:

The official documentation describes it like this:

每个从站的主节点必须是从节点的父节点(默认)或从节点的父节点的后代.这个限制是必要的,以保证从站可以放置在其主站的任何可见部分上,而没有从站被其父站剪辑的危险.

The master for each slave must either be the slave's parent (the default) or a descendant of the slave's parent. This restriction is necessary to guarantee that the slave can be placed over any part of its master that is visible without danger of the slave being clipped by its parent.

如果你想让树视图在 f 中,最简单的方法是让 f 成为父视图:

If you want the treeview to be in f, the simplest way is to make f be the parent:

self.tree = ttk.Treeview(f, ...)
self.tree.grid(row=0, column=0, sticky=NSEW)

这篇关于不能将 tkinter 小部件放在顶层框架内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆