【问题标题】:customtkinter - create frame with other class than the main frame but then assign it to the main framecustomtkinter - 使用除主框架以外的其他类创建框架,然后将其分配给主框架
【发布时间】:2022-11-02 05:45:12
【问题描述】:

我正在尝试在其他类中创建我的 customtkinter-app 的一部分,以便我可以将它们分配给 App 类。但我必须做错事。如您所见,目标是将标签文本从“self.label”放置到框架“self.frame_rot”的中间 请检查我的代码:


import tkinter
import customtkinter as ctk


class Frame1(ctk.CTkFrame):
    def __init__(self, master):
        super().__init__(master)
        
        self.frame_rot = ctk.CTkFrame(master, height = 100, width = 100, fg_color = 'red').grid(row = 0, column = 0)
        self.label = ctk.CTkLabel(self.frame_rot, text = 'hallo').place(relx = 0.5, rely = 0.5, anchor = 'center')
        
        
class App(ctk.CTk):
    def __init__(self):
        super().__init__()
        
        self.geometry('500x500')
        self.title('TestApp')
        
        self.frame = Frame1(self)

if __name__ == "__main__":
    app = App()
    app.mainloop()

我只是不知道从这里开始该怎么办!

【问题讨论】:

  • 实际上self.frame_rot 是None,因为它是.grid(...) 的结果,所以标签将被放在根窗口内。
  • tkinter.tK() 在哪里?

标签: python class tkinter inheritance customtkinter


【解决方案1】:

尝试这个:

import tkinter
import customtkinter as ctk

        
class App(ctk.CTk):
    def __init__(self):
        super().__init__()
        
        self.geometry('500x500')
        self.title('TestApp')
        
        self.frame = Frame1(self)

class Frame1(ctk.CTkFrame):
    def __init__(self, master):
        super().__init__(master)

        #if you remove this, the "red block" will stick to the upper left corner
        master.grid_columnconfigure(0, weight=1)
        master.grid_rowconfigure(0, weight=1)
        
        self.frame_rot = ctk.CTkFrame(master=master, fg_color='red')
        self.frame_rot.grid(row=0, column=0)

        #sticky "ns" will centralize the label vertically
        self.label = ctk.CTkLabel(master=self.frame_rot, text='hallo', height=130)
        self.label.grid(row=0, column=0, sticky="ns")
        

if __name__ == "__main__":
    app = App()
    app.mainloop()

【讨论】:

    猜你喜欢
    • 2010-11-14
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多