【问题标题】:PYTHON3: Label overlaps tabs in notebookPYTHON3:标签重叠笔记本中的标签
【发布时间】:2017-06-09 16:02:46
【问题描述】:

我对 python 和 tkinter 还很陌生,但是鉴于下面的小示例代码,为什么标签“Hello???”重叠应该是其父级的笔记本选项卡。

我的假设是笔记本作为标签的父级会导致第 0 行和第 0 列(包含 Frame )成为笔记本主体本身的左上角,但它似乎不是父级,相反 self.root 似乎是父级,尽管笔记本是作为父级传递的对象。

将标签网格属性设置为 row=1, column=0 并不能(在我的理解中是正确的)解决问题,因为第 0 行没有任何内容,因此会崩溃。出于同样的原因,将 note 设置为 row=1, column=0 似乎也没有做任何事情。也不会将 Page Frame 设置为 row=1, column=0。

任何帮助将不胜感激。

App.py:

#!/usr/bin/env python

from tkinter import Tk;
from tkinter.ttk import Frame, Notebook;
from page import Page;

class Application(Frame):
    def __init__(self, parent):
        super().__init__(parent);
        self.root = parent;
        self.root.geometry("450x450+500+500");
        self.root.rowconfigure(0, weight=1);
        self.root.columnconfigure(0, weight=1);
        self.root.grid();

        note = Notebook(self.root);

        page = Page(note, "pageOne");

        note.add(page, text="Page One");

        note.grid(sticky="NSEW"); #r=0, c=0 of main window


def main():
    root = Tk();
    Application(root);
    root.mainloop();

if __name__ == '__main__':
    main();

page.py:

#!/usr/bin/env python

from tkinter.ttk import Frame, Label;

class Page(Frame):
    def __init__(self, parent, name):
        super().__init__(parent);
        self.root = Frame(parent);
        self.name = name;
        self.root.grid(); #r=0, c=0 of notebook interior? 

        Label(self.root, text="Hello???").grid(); #r=0, c=0 also of notebook interior frame

【问题讨论】:

    标签: python user-interface tkinter tabs


    【解决方案1】:

    Page 中的所有内容都必须在页面中。您正在父级中创建self.root。这不是这些类的设计方式。将该行更改为:

    self.root = Frame(self)
    

    不过,我认为不需要self.root,因为self 已经是Frame

    【讨论】:

    • 很抱歉,我对 Python 还是很陌生,您能否详细说明一下“这些类的设计目的不是这样”。我已经投票并标记为答案,因为在 Page.py 中更改 self.root = Frame(self) 解决了这个问题(另外,完全删除 self.root 并且只使用 self 也解决了这个问题)
    • @Akidi:这个Page 类被设计成一个完全独立的单元。当它在父级中创建一个小部件时,它不再是自包含的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    相关资源
    最近更新 更多