Home > Enterprise >  TypeError in customtkinker module
TypeError in customtkinker module

Time:07-08

I'm testing customtkinter module, and I have a problem. I've made test gui and some code, which should execute when button is clicked. Unfortunately when I click button I have error:

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

That is not even in my code (I have only 250 lines). I've checked all types, and it's all good. Does anyone know how do deal with it?

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

CodePudding user response:

It says line 1883 because it the library line 1883 it giving a error because something is not correct in your script If I could see your code I could help solve this problem

CodePudding user response:

The error happens at this line

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

you cannot call a function as a string to fix it try this line

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