TypeError в модуле customtkinker

Я тестирую модуль customtkinter, и у меня возникла проблема. Я сделал тестовый графический интерфейс и некоторый код, который должен выполняться при нажатии кнопки. К сожалению, когда я нажимаю кнопку, у меня возникает ошибка:

      C:\Users\Jacek\Desktop>python test.py
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:\Python\Python38-32\lib\site-packages\customtkinter\widgets\ctk_button.py", line 390, in clicked
    self.function()
TypeError: 'str' object is not callable

Этого нет даже в моем коде (у меня всего 250 строк). Я проверил все типы, и все в порядке. Кто-нибудь знает, как с этим бороться?

      import tkinter
import customtkinter

customtkinter.set_appearance_mode("dark")  # Modes: system (default), light, dark
customtkinter.set_default_color_theme("blue")  # Themes: blue (default), dark-blue, green

gui = customtkinter.CTk()  # create CTk window like you do with the Tk window
gui.geometry("520x420")
gui.title("Layer cake calculator")


# FRAMES CONFIGURATION

frame_left = customtkinter.CTkFrame(master=gui,
                                    width=180,
                                    corner_radius=0)
frame_left.grid(row=0, column=0, sticky="nswe")

frame_right = customtkinter.CTkFrame(master=gui,
                                     width=180,)
frame_right.grid(row=0, column=1, sticky="nswe", padx=20, pady=20)


# WELCOME label

welcome_label = customtkinter.CTkLabel(master=frame_left,
                                       text="Welcome to bday cake calculator!",
                                       text_font=("Arial", 12)).grid(row=1,
                                                                     column=0,
                                                                     pady=10,
                                                                     padx=10)


# HOW MANY PORTIONS entry

portion_label = customtkinter.CTkLabel(master=frame_left,
                                       text="How many portions?").grid(row=2,
                                                                       column=0,
                                                                       sticky="w")
portion_entry = customtkinter.CTkEntry(master=frame_left,
                                        width=100).grid(row=3,
                                                        column=0,
                                                        sticky="w",
                                                        padx=20,
                                                        pady=5)



# PRICE entry

price_label = customtkinter.CTkLabel(master=frame_left, text="Price per person?").grid(row=4,
                                                                                       column=0,
                                                                                       sticky="w")
price_entry = customtkinter.CTkEntry(master=frame_left,
                             width=100).grid(row=5, column=0, sticky="w", padx=20, pady=5)


# WHICH FORM label

form_label = customtkinter.CTkLabel(master=frame_left, text="Which form?").grid(row=6,
                                                                                column=0,
                                                                                sticky="w",
                                                                                pady=5)
form_combobox = customtkinter.CTkComboBox(master=frame_left,
                                     width=100,
                                     values=["Round", "Square", "Rectangle"]).grid(row=7,
                                                                                   column=0,
                                                                                   sticky="w",
                                                                                   pady=5,
                                                                                   padx=20)


# MORE INFO

more_info_label = customtkinter.CTkLabel(master=frame_left, text="More info:").grid(row=8,
                                                                                    column=0,
                                                                                    sticky="w",
                                                                                    pady=5)


# GLUTENFREE checkbox

gf = tkinter.BooleanVar()
gf_check = customtkinter.CTkCheckBox(master=frame_left,
                          variable=gf,
                          text="Gluten free?")
gf_check.deselect()
gf_check.grid(row=9, column=0, sticky="w", padx=20, pady=5)


# BOX INCLUDED checkbox

box = tkinter.BooleanVar()
box_check = customtkinter.CTkCheckBox(master=frame_left,
                           variable=box,
                           text="Box included?")
box_check.deselect()
box_check.grid(row=10, column=0, sticky="w", padx=20, pady=5)

# FUNCTIONS

def print_test():  # TEST FUNCTION
    print(portion_entry.get())
    print(price_entry.get())
    print(form_combobox.get())              ####ACTIVE
    print(gf.get())
    print(box.get())


def price_calculator():  # Price for the cake
    portion = int(portion_entry.get())
    price_per_person = int(price_entry.get())
    price_calc = portion * price_per_person

    if gf.get():
        price_calc += (portion * 2)

    if box.get():
        box_price = 0
        if portion <= 20:
            box_price = 7  # TODO sprawdzić ceny opakowań
        elif portion <= 30:
            box_price = 10
        elif portion <= 40:
            box_price = 15
        elif portion > 40:
            box_price = 20
        price_calc += box_price

    print("Prize for cake: ", price_calc, " zł")
    if box.get():
        print("That includes box price -", box_price, " zł")

    # if gf.get():
    #     print("And gf bonus: ", gf_bonus, " zł")


def round_calc():  # Round cake size calculator
    portion = int(portion_entry.get())
    pi = round(math.pi, 2)
    one_piece = 22.8

    def circle_field_calc(portion):
        circle_field = portion * one_piece
        return circle_field

    def d_calc(portion, circle_field):
        d = 2 * math.sqrt(circle_field / pi)
        d = round(d)
        return [d, circle_field]

    if portion < 31:  # cake with one layer
        print("Diameter size in cm = ", d_calc(portion, circle_field_calc(portion))[0])
    elif 31 <= portion <= 50:  # cake with two layers
        first_layer = d_calc(portion, circle_field_calc(portion))[1] * 0.65  # 65 % of cake field is first layer field
        second_layer = d_calc(portion, circle_field_calc(portion))[1] * 0.35  # 35 % of cake field is second layer field
        first_d = d_calc(portion, first_layer)[0]  # calculating d in first layer
        second_d = d_calc(portion, second_layer)[0]  # calculating d in second layer
        print("Diameter size of first layer = ", first_d, "cm,\n",
              "\t\t\t\tand second layer =", second_d, "cm.")
    elif 51 <= portion <= 75:  # cake with three layers
        first_layer = d_calc(portion, circle_field_calc(portion))[
                          1] * 0.465  # ~46,5 % of cake field is first layer field
        second_layer = d_calc(portion, circle_field_calc(portion))[
                           1] * 0.32  # ~32 % of cake field is second layer field
        third_layer = d_calc(portion, circle_field_calc(portion))[1] * 0.24  # ~24 % of cake field is third layer field
        first_d = d_calc(portion, first_layer)[0]  # calculating d in first layer
        second_d = d_calc(portion, second_layer)[0]  # calculating d in second layer
        third_d = d_calc(portion, third_layer)[0]  # calculating d in third layer
        print("Diameter size of first layer = ", first_d, "cm,\n",
              "\t\t\t\tsecond layer =", second_d, "cm,\n",
              "\t\t\t\tand third layer =", third_d, "cm.")



def one_portion_field(side):  # With this function you can set side size of one portion
    one_portion_field = side ** 2
    return one_portion_field  # Field size of portion for one person


def square_calc():
    portion = int(portion_entry.get())
    square_cake_size = one_portion_field(6) * portion

    if portion <= 30:  # 1 layers
        cake_side = math.sqrt(square_cake_size)
        print("Side of square cake: ", round(cake_side), " cm")
    elif 31 < portion <= 50:  # 2 layers
        square_1 = square_cake_size * 0.65
        square_2 = square_cake_size * 0.35
        cake_side_1 = round(math.sqrt(square_1))
        cake_side_2 = round(math.sqrt(square_2))
        print("Side of first layer: ", round(cake_side_1), "cm,\n",
              "\tand second layer: ", round(cake_side_2), "cm,\n")
    elif 51 <= portion <= 75:  # 3 layers
        square_1 = square_cake_size * 0.46
        square_2 = square_cake_size * 0.32
        square_3 = square_cake_size * 0.24
        cake_side_1 = round(math.sqrt(square_1))
        cake_side_2 = round(math.sqrt(square_2))
        cake_side_3 = round(math.sqrt(square_3))
        print("Side of first layer: ", round(cake_side_1), "cm,\n",
              "\tsecond layer: ", round(cake_side_2), "cm,\n",
              "\tand third layer: ", round(cake_side_3), "cm,\n")



def rectangle_calc():
    portion = int(portion_entry.get())
    one_portion_field(6)
    cake_size = one_portion_field(6) * portion
    cake_side_a = round(math.sqrt(cake_size / 0.7))
    cake_side_b = round(cake_side_a - 0.3 * cake_side_a)

    if portion > 25:  # 2 layers
        layer_1 = cake_size * 0.65
        layer_2 = cake_size * 0.35
        cake_side_a_1 = round(math.sqrt(layer_1 / 0.7))
        cake_side_b_1 = round(cake_side_a_1 - 0.3 * cake_side_a_1)
        cake_side_a_2 = round(math.sqrt(layer_2 / 0.7))
        cake_side_b_2 = round(cake_side_a_2 - 0.3 * cake_side_a_2)
        print("Sides of first layer: ", round(cake_side_a_1), " cm X ", round(cake_side_b_1), "cm,\n",
              "\t\tand second layer: ", round(cake_side_a_2), " cm X ", round(cake_side_b_2), "cm,\n")
    elif portion > 45:  # 3 layers
        layer_1 = cake_size * 0.46
        layer_2 = cake_size * 0.32
        layer_3 = cake_size * 0.24
        cake_side_a_1 = round(math.sqrt(layer_1 / 0.7))
        cake_side_b_1 = round(cake_side_a_1 - 0.3 * cake_side_a_1)
        cake_side_a_2 = round(math.sqrt(layer_2 / 0.7))
        cake_side_b_2 = round(cake_side_a_2 - 0.3 * cake_side_a_2)
        cake_side_a_3 = round(math.sqrt(layer_3 / 0.7))
        cake_side_b_3 = round(cake_side_a_3 - 0.3 * cake_side_a_3)
        print("Sides of first layer: ", round(cake_side_a_1), " cm X ", round(cake_side_b_1), "cm,\n",
              "\t\tsecond layer: ", round(cake_side_a_2), " cm X ", round(cake_side_b_2), "cm,\n",
              "\t\tthird layer: ", round(cake_side_a_3), " cm X ", round(cake_side_b_3), "cm,\n")


def button_func():
    price_calculator()
    if form_combobox.get("active") == "Round":
        round_calc()
    elif form_combobox.get("active") == "Square":
        square_calc()
    elif form_combobox.get("active") == "Rectangle":
        rectangle_calc()


# BUTTON

button = customtkinter.CTkButton(master=frame_left, text="Enter", command="print_test").grid(row=11, column=0, padx=20, pady=15)

gui.mainloop()
#python test.py

2 ответа

В нем говорится о строке 1883, потому что это строка библиотеки 1883, она выдает ошибку, потому что что-то не так в вашем сценарии. Если бы я мог видеть ваш код, я мог бы помочь решить эту проблему.

Ошибка возникает в этой строке

      button = customtkinter.CTkButton(master=frame_left, text="Enter", command="print_test").grid(row=11, column=0, padx=20, pady=15)

вы не можете вызвать функцию как строку, чтобы исправить это, попробуйте эту строку

      button = customtkinter.CTkButton(master=frame_left, text="Enter", command=print_test()).grid(row=11, column=0, padx=20, pady=15)
Другие вопросы по тегам