我正在测试customtkinter模块,我有一个问题.我制作了测试gui和一些代码,当点击按钮时应该会执行.不幸的是,当我点击按钮时,我出现了错误:

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

推荐答案

错误发生在这一行

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

不能将函数作为字符串调用来修复它,请try 此行

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

Python相关问答推荐

Python中的嵌套Ruby哈希

如何在Windows上用Python提取名称中带有逗号的文件?

如何让程序打印新段落上的每一行?

如何列举Pandigital Prime Set

对所有子图应用相同的轴格式

numpy卷积与有效

如何在turtle中不使用write()来绘制填充字母(例如OEG)

我的字符串搜索算法的平均时间复杂度和最坏时间复杂度是多少?

可以bcrypts AES—256 GCM加密损坏ZIP文件吗?

Matplotlib中的字体权重

下三角形掩码与seaborn clustermap bug

将标签移动到matplotlib饼图中楔形块的开始处

浏览超过10k页获取数据,解析:欧洲搜索服务:从欧盟站点收集机会的微小刮刀&

Django.core.exceptions.SynchronousOnlyOperation您不能从异步上下文中调用它-请使用线程或SYNC_TO_ASYNC

将数字数组添加到Pandas DataFrame的单元格依赖于初始化

如何在表单中添加管理员风格的输入(PDF)

将索引表转换为Numy数组

正则表达式反向查找

Pandas:根据相邻行之间的差异过滤数据帧

Python:使用asyncio.StreamReader.readline()读取长行