简体   繁体   English

外部 window 为空,带有“tk.Toplevel”和“parent”

[英]External window is empty with "tk.Toplevel" and "parent"

When I click on a label to open an external window ( tertiary .py), the window opens normally but is empty inside.当我单击 label 打开外部 window(第三级.py)时,window 正常打开,但内部为空。 I don't get errors, but I don't see Tkinter widgets and miscellaneous items.我没有收到错误,但我没有看到 Tkinter 小部件和杂项。

This is the secondary window where there is the label on which I click in order to open an external window.这是辅助window,其中有 label,我单击它以打开外部 window。 I add, for information purposes only, that this window is called secondary because it is displayed in a frame (with a vertical menu on the left) where the main window is called home = tk.Tk() .我补充说,仅供参考,这个 window 被称为辅助,因为它显示在一个框架中(左侧有一个垂直菜单),其中window 被称为home = tk.Tk() The secondary window opens and displays correctly in the frame.辅助window 打开并在框架中正确显示。

Inside the secondary window there is the label thanks to which I want to open an external window (not in the frame) called tertiary二级window 内有 label 多亏了它,我想打开一个名为三级的外部 window(不在框架中)

How can I solve?我该如何解决?

secondary.py二级.py

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

import other_options
from other_options import form_other_options

class Page2(tk.Frame):
    def __init__(self, master, **kw):
        super().__init__(master, **kw)

        self.configure(bg='white')
        bar1=Frame(self, width=2200, height=35, background="#960000", highlightbackground="#b40909", highlightthickness=0) #grigio chiaro #b40909
        bar1.place(x=0, y=0)

        other_options = Label(self, text="Other Options", bg="#960000", foreground='white', font='Ubuntu 10')
        other_options.place(x=1025, y=7)
        other_options.bind("<Button-1>", lambda event: other_options.form_other_options(self))

tertiary.py三级.py

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

def form_other_options(parent):

    other_options = tk.Toplevel(parent)
    other_options.title("Other options")
    other_options.geometry("1000x800")
    other_options.config(bg="white")
    other_options.state("normal")

    other_options.transient(parent)


    class checkbox():
        def __init__(self, master, **kw):
            super().__init__(master, **kw)

            labelframe1 = LabelFrame(self, text="checkbox", width=600,height=190, bg="white", foreground='#e10a0a')
            labelframe1.place(x=10, y=13)

            Checkbutton1 = IntVar()

            Button1 = Checkbutton(self, text = "option1",
                    variable = Checkbutton1,
                    onvalue = 1,
                    offvalue = 0,
                    height = 2,
                    width = 10)

            Button1.pack()

    other_options.mainloop()

The first reason why the window is empty is that you never create an instance of the class checkbox , so the code that creates the widgets never runs. window 为空的第一个原因是您永远不会创建 class checkbox的实例,因此创建小部件的代码永远不会运行。

So, the first step is to make sure you create an instance of that class:因此,第一步是确保创建该 class 的实例:

other_options = tk.Toplevel(parent)
...
cb = checkbox(other_options)

When you do that, you will discover other bugs in your code.当您这样做时,您会发现代码中的其他错误。 The first positional argument when creating a tkinter widget must be another widget.创建 tkinter 小部件时的第一个位置参数必须是另一个小部件。 If you want the widget to be inside a Toplevel , the parent needs to be the toplevel or a descendant.如果您希望小部件位于Toplevel内,则父级必须是顶层或后代。 You're using self which isn't a widget at all.您使用的self根本不是小部件。

A simple fix is to use master instead of self , since you're passing the toplevel window as the master parameter:一个简单的解决方法是使用master而不是self ,因为您将顶层 window 作为master参数传递:

labelframe1 = LabelFrame(master, ...)
Button1 = Checkbutton(master, ...)

You also need to remove the call to other_options.mainloop() .您还需要删除对other_options.mainloop()的调用。 Unless there is a very compelling reason to do otherwise, an entire application should only call mainloop() exactly once.除非有非常令人信服的理由不这样做,否则整个应用程序应该只调用一次mainloop()


There are other problems in the code that, strictly speaking, aren't related to the question being asked.代码中还有其他问题,严格来说,与所提出的问题无关。 For example, you're importing tkinter twice: once as import tkinter as tk and once as from tkinter import * .例如,您要导入 tkinter 两次:一次是import tkinter as tk ,一次是from tkinter import * You only need to import tkinter once, and PEP8 1 recommends to use the first form:只需要导入一次tkinter,PEP8 1推荐使用第一种形式:

import tkinter as tk

You'll need to add the prefix tk.您需要添加前缀tk. to every place where you call a tkinter function.到您调用 tkinter function 的每个地方。 This will help make your code easier to understand and helps prevent pollution of the global namespace.这将有助于使您的代码更易于理解,并有助于防止污染全局命名空间。

You also should use PEP8 naming standards.您还应该使用PEP8命名标准。 Specifically, class names should begin with an uppercase character.具体来说,class 名称应以大写字符开头。 This will also make your code easier to understand.这也将使您的代码更易于理解。

There's also no reason why your checkbox class should be indented.您的checkbox class 也没有理由缩进。 You should move it out of the function.您应该将其移出 function。

Also, instead of checkbox not inheriting from anything, and then creating a LabelFrame , you should just inherit from LabelFrame .此外,您应该只从LabelFrame继承,而不是checkbox不从任何东西继承,然后创建一个LabelFrame That way you can use your class more like a real widget, and would enable you to use self rather than master when creating the other widgets.这样您就可以像使用真正的小部件一样使用 class,并且可以让您在创建其他小部件时使用self而不是master

class Checkbox(tk.LabelFrame):
    def __init__(self, master, **kw):
        super().__init__(master, **kw)
        ...
        button1 = tk.Checkbutton(self, ...)
        ...

There are probably other problems, but those are the most obvious.可能还有其他问题,但这些是最明显的。

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

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