为什么第一个代码在定义后运行而第二个代码不运行?

Why the 1st code runs and 2nd doesn't after defining it?

第一个代码完全可以正常工作。

但是当我定义它时,即使它成为一个函数(如下面的代码),它不起作用。它甚至没有显示任何错误。我不明白这个问题的原因。

我是不是犯了什么错误导致它无法正常工作? ______________________

您正在重新定义 win,然后对其执行操作,这似乎不太合适。如果你想清除屏幕并放置新的小部件,我会这样做:

from tkinter import *
from tkinter import ttk

win=Tk()  

def code():
    btn1.destroy()
    add=StringVar()
    sub=StringVar()
    pro=StringVar()

    def Find():
        add.set(float(ent00.get())+float(ent01.get()))
        sub.set(float(ent00.get())-float(ent01.get()))
        pro.set(float(ent00.get())*float(ent01.get()))

    ent00=Entry(win, width=15)
    ent00.grid(row=4, column=1, padx=10, pady=10, sticky='w')
    ent01=Entry(win, width=15)
    ent01.grid(row=5, column=1, padx=10, pady=10, sticky='w')

    lbl8=Label(win, text="Add", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=6, column=0, padx=20, pady=10, sticky='w')
    ent8=Entry(win, textvariable=add, width=15, state='readonly')
    ent8.grid(row=6, column=1, padx=10, pady=10, sticky='w')

    lbl15=Label(win, text="Subtract", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=7, column=0, padx=20, pady=10, sticky='w')
    ent15=Entry(win, textvariable=sub, width=15, state='readonly')
    ent15.grid(row=7, column=1, padx=10, pady=10, sticky='w')

    lbl9=Label(win, text="Product", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=8, column=0, padx=20, pady=10, sticky='w')
    ent9=Entry(win, textvariable=pro, width=15, state='readonly')
    ent9.grid(row=8, column=1, padx=10, pady=10, sticky='w')

    btn = Button(win, text = 'Calculate', command=Find, bd = '5', width=15, height=2)
    btn.grid(row=11, column=1, padx=20, pady=10)


btn1 = Button(win, text = 'OPEN CODE', command=code, bd = '5', width=20, height=2)
btn1.grid(row=11, column=1, padx=20, pady=10)
win.geometry("1400x700+250+250")
win.mainloop()

我刚刚删除了 code 函数中修改 win 的三行并添加了 btn1.destroy(),这从 window 中删除了我们要停止显示的按钮。