我正在尝试从 CustomTkinter 网站/github 修改complex_example.py,以便我可以让它最终修改鱼喂食机中使用的一些文件参数。
当我在 PyCharm 中运行代码时,我得到了我期望的窗口(忽略明显的大小问题)。
但是,每当我单击任何按钮(例如“取消”按钮)时,我都会收到以下错误
文件 “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/init.py”, 第 2345 行,位于 getattr 返回 getattr(self.tk, attr)
AttributeError:“_tkinter.tkapp”对象没有属性 'sidebar_button_event'
奇怪的是,当我运行原始的complex_example.py时,按钮没有问题。
我确信这个问题是显而易见的,但我仍在学习 Python,而 Google 并不是我的朋友。
任何帮助将不胜感激。
import sys
#import tkinter
#import tkinter.messagebox
import customtkinter
customtkinter.set_appearance_mode("System") # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
class App(customtkinter.CTk):
current_speed = 0
current_direction = "Clockwise"
current_steps = 0
def __init__(self):
super().__init__()
# Begin row count
r=0
# Set column to 0
c=0
# Set speed entry
current_speed=0
# Set direction
current_direction=f"Clockwise"
# Set steps per feeding
current_steps_feeding=200
# configure window
self.title("Motor Options")
self.geometry(f"{750}x{385}")
# configure grid layout (4x4)
# self.grid_columnconfigure((0, 1, 2, 3), weight=1)
# self.grid_rowconfigure((0, 1, 2), weight=1)
# create sidebar frame with widgets
self.sidebar_frame = customtkinter.CTkFrame(self, width=750, corner_radius=0)
self.sidebar_frame.grid(row=0, column=c, rowspan=3, sticky="nsew")
# self.sidebar_frame.grid_rowconfigure(9, weight=1)
self.logo_label = customtkinter.CTkLabel(self.sidebar_frame, text="Motor Options", font=customtkinter.CTkFont(size=20, weight="bold"))
self.logo_label.grid(row=r, column=c, columnspan=4, padx=20, pady=(10,10))
r +=1
self.logo_label = customtkinter.CTkLabel(self.sidebar_frame, text="Speed", font=customtkinter.CTkFont(size=20, weight="bold"))
self.logo_label.grid(row=r, column=c, padx=20, pady=(10,10))
# Speed up
c +=1
self.sidebar_button_1 = customtkinter.CTkButton(self.sidebar_frame, command=lambda: self.sidebar_button_event(1))
self.sidebar_button_1.grid(row=r, column=c, padx=20, pady=(10,10))
# Speed down
c +=1
self.sidebar_button_2 = customtkinter.CTkButton(self.sidebar_frame, command=lambda: self.sidebar_button_event(2))
self.sidebar_button_2.grid(row=r, column=c, padx=20, pady=(10,10))
# create Speed entry box
c +=1
self.entry_speed = customtkinter.CTkEntry(self.sidebar_frame, placeholder_text=current_speed)
self.entry_speed.grid(row=r, column=c, padx=20, pady=(10,10))
# Cancel
r +=1
self.sidebar_button_16 = customtkinter.CTkButton(self.sidebar_frame, command=lambda: self.sidebar_button_event(16))
self.sidebar_button_16.grid(row=r, column=c, padx=20, pady=(10,10))
# set default values
self.sidebar_button_1.configure(text="Faster")
self.sidebar_button_2.configure(text="Slower")
self.sidebar_button_16.configure(text="Cancel")
def sidebar_button_event(buttonNum):
if buttonNum == 1:
print(f"Faster")
print(f"Attempt to increase speed beyond 100, speed set to 100")
elif buttonNum == 2:
print(f"Slower")
elif buttonNum == 16:
print(f"Cancel")
sys.exit(0)
if __name__ == "__main__":
app = App()
app.mainloop()