关于python和customtkinter的问题

0 投票
1 回答
42 浏览
提问于 2025-04-13 01:01

代码:

from customtkinter import *
from tkinter import *

def createCardFunc():

    def createFunc():
        title = titleEntry.get()
        description = descEntry.get("1.0", "end-1c")
        cc.destroy()

        r = CTk()

        r.geometry(f"1500x850")
        r.title("NH - Create Note")
        r.iconbitmap("Assets/Images/Logo/nh-iconbitmap.ico")
        r.resizable(False, False)

        titleLabel = CTkLabel(r, text=title, font=("Calibri", 30, 'bold'))
        titleLabel.grid(row=0, column=1, sticky="w")

        r.mainloop()

    cc = CTk()
    cc.iconbitmap("Assets/Images/Logo/nh-iconbitmap.ico")
    cc.title("NH - Create Card")
    screen_width = 500
    screen_height = 500
    cc.geometry(f"{screen_width}x{screen_height}")
    cc.resizable(False, False)

    addTitle = CTkLabel(cc, text="Title:", font=("Calibri", 25))
    addTitle.grid(row=0, column=0)

    titleEntry = CTkEntry(cc, font=("Courier New", 20, 'bold'), width=screen_width - 125)
    titleEntry.grid(row=0, column=1)

    descLab = CTkLabel(cc, text="Description:", font=("Calibri", 25))
    descLab.grid(row=1, column=0)

    descEntry = CTkTextbox(cc, font=("Courier New", 20, 'bold'), width=(screen_width // 2) + 108, wrap="word")
    descEntry.grid(row=2, column=1, sticky="w", padx=(10, 0))

    submit = CTkButton(cc, text="Create", font=("Calibri", 20), command=createFunc)
    submit.grid(row=3, column=1, sticky="w", padx=(10, 0), pady=(70, 0))

    cc.mainloop()



if __name__ == '__main__':
    createCardFunc()

错误:

invalid command name "3115937643968update"
    while executing
"3115937643968update"
    ("after" script)
invalid command name "3115937643584check_dpi_scaling"
    while executing
"3115937643584check_dpi_scaling"
    ("after" script)
invalid command name "3115937641664_click_animation"
    while executing
"3115937641664_click_animation"
    ("after" script)
invalid command name "3115897138688<lambda>"
    while executing
"3115897138688<lambda>"
    ("after" script)

你可以看到,我在“r”这个根节点上添加了一些小部件。但是每次我运行这个命令时,根本看不到这些小部件。

这个错误只在我点击“提交”按钮的时候出现。

我尝试重新安装customtkinter,重启电脑,结果都没用。

1 个回答

0

我觉得问题出在createFunc函数里创建新窗口的地方。就像这里说的:在Python中可以有两个tkinter实例吗?,你不能同时使用两个CTk实例(Tk和CTk的工作方式是一样的)。

你应该使用TopLevel(或者如果你真的不想这样做,可以启动另一个Python脚本,但这并不推荐)来实现你想要的功能。

希望我能帮到你,祝你有美好的一天。

关注评论,看看真正的问题和答案是什么

撰写回答