pycharm 名称“窗口”是没有定义的错误是未来,而执行上述代码

ryoqjall  于 2023-05-17  发布在  PyCharm
关注(0)|答案(1)|浏览(155)
def create_window():

    window = ctk.CTk()
    window.title("CREATE")
    window.geometry("1789x760")

create_frame = ctk.CTkFrame(window, width=600, height=300)
create_frame.pack()

create_label = ctk.CTkLabel(create_frame, text="Sample")
create_label.pack()

window.mainloop()
ovfsdjhp

ovfsdjhp1#

您可以尝试以下功能方法:

def populate_window(root_window):
    create_frame = ctk.CTk.Frame(root_window, width=600, height=300)
    create_frame.pack()
    create_label = ctk.CTkLabel(create_frame, text="Sample")
    create_label.pack()

window = ctk.CTk()
window.title('CREATE')
window.geometry('1789x760')
window.mainloop()

这是可行的,但是,您将无法轻松地重用populate_window函数。
这种面向对象的方法将允许更多的可移植性和代码重用:

class Window(ctk.CTkFrame):
    def __init__(self, parent):
        self.parent = parent
        ctk.CTkFrame.__init__(self, self.parent)
        self.pack()
        self.populate_window()

    def populate_window(self):
        create_label = ctk.CTkLabel(self, text="Sample")
        create_label.pack()

if __name__ == '__main__':
    root = ctk.CTk()
    root.title("CREATE")
    root.geometry('1789x760')
    window = Window(root)
    root.mainloop()

相关问题