1. 此代码不会在主窗口中显示任何小部件.有什么解决方案吗?

  2. 有没有办法在一个模块中创建多个顶级窗口?目前,我正在 for each 顶级窗口创建新的python模块.

  3. 此外,在创建了一个新的顶级窗口后,我们是使用mainloop还是update或update\u ideltasks?

非常感谢.

    class MainWindowWidgets(ttk.Frame):
        def __int__(self, container):
           super().__init__(container)
    
           create_frame = ttk.LabelFrame(container, text='New Frame')
           create_frame.grid(row=0, column=0)
    
           create_btn = ttk.Button(create_frame, text='New Button')
           create_btn.grid(row=0, column=0)
    
    
    class App(tk.Tk):
        def __init__(self):
            super().__init__()
            self.title('My App')
            self.geometry('1000x500')
            self.resizable(False, False)
            main_window_widgets = MainWindowWidgets(self)
            main_window_widgets.pack()
    
    
    # This function is called from login window if login is successful and login window is destroyed

    def open_main_window():
        app = App()
        app.mainloop()

推荐答案

在你的MainWindowWidgets个班里有两个问题:

  • __int__(...)应该是__init__(...)
  • 应使用self而不是container作为create_frame的父项
class MainWindowWidgets(ttk.Frame):
    # __int__ should be __init__ instead
    #def __int__(self, container):
    def __init__(self, container):
       super().__init__(container)

       # use self instead of container as the parent
       #create_frame = ttk.LabelFrame(container, text='New Frame')
       create_frame = ttk.LabelFrame(self, text='New Frame')
       create_frame.grid(row=0, column=0)

       create_btn = ttk.Button(create_frame, text='New Button')
       create_btn.grid(row=0, column=0)

Python相关问答推荐

将两只Pandas rame乘以指数

如何使用数组的最小条目拆分数组

当从Docker的--env-file参数读取Python中的环境变量时,每个\n都会添加一个\'.如何没有额外的?

我如何使法国在 map 中完全透明的代码?

Python中绕y轴曲线的旋转

将输入聚合到统一词典中

如何根据一列的值有条件地 Select 前N组?

python中的解释会在后台调用函数吗?

Pandas GroupBy可以分成两个盒子吗?

如何在FastAPI中为我上传的json文件提供索引ID?

Django admin Csrf令牌未设置

在极中解析带有数字和SI前缀的字符串

python sklearn ValueError:使用序列设置数组元素

Pandas—堆栈多索引头,但不包括第一列

数据框,如果值在范围内,则获取范围和

如何使用pytest在traceback中找到特定的异常

替换包含Python DataFrame中的值的<;

我可以同时更改多个图像吗?

如何在Python中画一个只能在对角线内裁剪的圆?

`Convert_time_zone`函数用于根据为极点中的每一行指定的时区检索值