我使用的是定制化的,这和剪贴机完全一样,但更漂亮。
当类继承自tkinter中的框架类时,如何设置tkinter框架类的主目录??
我的问题是,我希望frame_right成为wave_prop_inputs框架的主人;这基本上意味着,框架中的一个框架。
我尝试在创建master=frame_right类的对象时使用wave_prop_inputs,但这给了错误init()一个意外的关键字参数‘主’
这是我的主要文件:
import customtkinter as ctk
import main
import a_wave_prop.a_view as wave_prop
from constants import TITLE, WIDTH, HEIGHT
#Window Visuals
ctk.set_appearance_mode("System") # Modes: "System" (standard), "Dark", "Light"
ctk.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
#Main Window
class App(ctk.CTk):
def __init__(self):
super().__init__()
self.title(TITLE)
self.geometry(f"{WIDTH}x{HEIGHT}")
self.protocol("WM_DELETE_WINDOW", self.on_closing) # call .on_closing() when app gets closed
self.grid_columnconfigure(1, weight=1)
self.grid_rowconfigure(0, weight=1)
#Create Two Main Frames
#Left Frame
self.frame_left = main.main_left()
self.frame_left.configure(width=180, corner_radius=0)
self.frame_left.grid_rowconfigure(0, minsize=10) # empty row with minsize as spacing
self.frame_left.grid_rowconfigure(8, weight=1) # empty row as spacing
self.frame_left.grid(row=0, column=0, sticky="nswe")
#Right Frame
self.frame_right = main.main_right()
self.frame_right.rowconfigure((1), weight=1)
self.frame_right.columnconfigure((0, 1, 2, 3, 4, 5, 6, 7), weight=1)
self.frame_right.grid(row=0, column=1, sticky="nswe", padx=10, pady=10)
#Right Frame Children
#wave_prop Inputs Frame
self.wave_prop_inputs = wave_prop.inputs(master=self.frame_right)
self.wave_prop_inputs.grid(row=0, column=0, columnspan=8, rowspan=1, pady=10, padx=10, sticky="nsew")
def on_closing(self, event=0):
self.destroy()
if __name__ == "__main__":
app = App()
app.mainloop()这是我的第二个档案:
import customtkinter as ctk
class inputs(ctk.CTkFrame):
def __init__(self):
super().__init__()
self.label_1 = ctk.CTkLabel(self, text="Incident Wave Inputs",
text_font=("Roboto Medium", -24,))
self.label_1.grid(row=1, column=0, columnspan=2, sticky='W', pady=10, padx=10)
self.label_2 = ctk.CTkLabel(self, text="Wave Height (H) = ",
text_font=("Roboto Medium", -16))
self.label_2.grid(row=2, column=0, sticky='W', pady=10, padx=10)
self.label_3 = ctk.CTkLabel(self, text="Water Depth (d) = ",
text_font=("Roboto Medium", -16))
self.label_3.grid(row=3, column=0, sticky='W', pady=10, padx=10)
self.label_4 = ctk.CTkLabel(self, text="Wave Period (T) = ",
text_font=("Roboto Medium", -16))
self.label_4.grid(row=4, column=0, sticky='W', pady=10, padx=10)
self.label_5 = ctk.CTkLabel(self, text="Wave Angle (\u03B1) = ",
text_font=("Roboto Medium", -16))
self.label_5.grid(row=2, column=2, sticky='E', pady=10, padx=10)
self.label_6 = ctk.CTkLabel(self, text="Bed Slope (s) = ",
text_font=("Roboto Medium", -16))
self.label_6.grid(row=3, column=2, sticky='E', pady=10, padx=10)
self.label_7 = ctk.CTkLabel(self, text="Depth of Interest (z) = ",
text_font=("Roboto Medium", -16))
self.label_7.grid(row=4, column=2, sticky='E', pady=10, padx=10)
self.entry2 = ctk.CTkEntry(self, placeholder_text="meter", width=120,
height=25, border_width=2, corner_radius=10)
self.entry2.grid(row=2, column=1, sticky='W', pady=10, padx=10)
self.entry3 = ctk.CTkEntry(self, placeholder_text="meter", width=120,
height=25, border_width=2, corner_radius=10)
self.entry3.grid(row=3, column=1, sticky='W', pady=10, padx=10)
self.entry4 = ctk.CTkEntry(self, placeholder_text="second", width=120,
height=25, border_width=2, corner_radius=10)
self.entry4.grid(row=4, column=1, sticky='W', pady=10, padx=10)
self.entry5 = ctk.CTkEntry(self, placeholder_text="degree", width=120,
height=25, border_width=2, corner_radius=10)
self.entry5.grid(row=2, column=3, sticky='W', pady=10, padx=10)
self.entry6 = ctk.CTkEntry(self, placeholder_text="meter/meter", width=120,
height=25, border_width=2, corner_radius=10)
self.entry6.grid(row=3, column=3, sticky='W', pady=10, padx=10)
self.entry7 = ctk.CTkEntry(self, placeholder_text="- meter", width=120,
height=25, border_width=2, corner_radius=10)
self.entry7.grid(row=4, column=3, sticky='W', pady=10, padx=10)
self.button_6 = ctk.CTkButton(self, text="Calculate", command=self.button_event)
self.button_6.grid(row=5, column=0, pady=10, padx=10, sticky="W")
def button_event(self):
print("Button pressed")发布于 2022-10-11 21:49:08
在这一行中,您尝试解析关键字参数母版:
wave_prop.inputs(master=self.frame_right)对于不需要任何类型参数的类:
class inputs(ctk.CTkFrame):
def __init__(self):
super().__init__() 若要使该类能够接受具有默认值的参数,您需要将其更改如下:
class inputs(ctk.CTkFrame):
def __init__(self, master=None):最后,但同样重要的是,您需要将该值解析为您的超类,以便使超类使用它。
super().__init__(master) 您应该看看关于OOP的基本教程,以便更多地受益于这个概念。此外,我建议您使用PEP-8 (Python样式指南),例如,请参阅类的命名约定。您的类看起来像一个常规python开发人员的方法。
https://stackoverflow.com/questions/74033956
复制相似问题