python标签不实时更新

python标签不实时更新

This question already has answers here:
Why is Button parameter “command” executed when declared?
                                
                                    (4个答案)
                                
                        
                                2年前关闭。
            
                    
我解决了具有相同问题的问题,而解决方案主要是在.set ()中。

我是新手,我做了同样的事情,但没有成功。请帮助理解并指出错误。

我这样做是为了使dp1o中的值根据公式xmax进行更改,但似乎仅对自动分配的零进行计算。

from math import *
from tkinter import *

root = Tk()
f_top = Frame(root)
root.title('Полет ядра')
g = 9.81
dx = 0.01
v = IntVar()
an = IntVar()
t = IntVar()
vx = IntVar()

def analysis():
    v = int(entry1.get())
    an = int(entry2.get())


    t = (((2 * v * sin(an)) / g))
    vx = v * cos(an)

    xmax = vx * t * cos(an)

    dp1o.config(text=xmax)


first = LabelFrame(root, text='Данные для первого графика')
first.pack(side='left')

second = Label(first, text='Начальная скорость')
second.pack()

entry1 = Entry(first, width=10, textvariable=v)
entry1.pack()

third = Label(first, text='Угол выстрела')
third.pack()

entry2 = Entry(first, width=10, textvariable=an)
entry2.pack()


first = LabelFrame(root, text='Расчет')
first.pack(side='right')
dp1 = Label(first, text='Дальность полета 1 тела')
dp1.pack()
dp1o = Label(first, text='')
dp1o.pack()


button = Button(root, text="Сгенерировать", command=analysis())
button.pack(side='left')

root.mainloop()

最佳答案

您的问题是您在()之后通过了analysis。处理命令时,无需在要调用的函数后放置()。

这是您的代码的有效版本。

from math import *
from tkinter import *

root = Tk()
f_top = Frame(root)
root.title('Полет ядра')
g = 9.81
dx = 0.01
v = IntVar()
an = IntVar()
t = IntVar()
vx = IntVar()

def analysis():
    v = int(entry1.get())
    an = int(entry2.get())


    t = (((2 * v * sin(an)) / g))
    vx = v * cos(an)

    xmax = vx * t * cos(an)

    dp1o.config(text=xmax)


first = LabelFrame(root, text='Данные для первого графика')
first.pack(side='left')

second = Label(first, text='Начальная скорость')
second.pack()

entry1 = Entry(first, width=10, textvariable=v)
entry1.pack()

third = Label(first, text='Угол выстрела')
third.pack()

entry2 = Entry(first, width=10, textvariable=an)
entry2.pack()


first = LabelFrame(root, text='Расчет')
first.pack(side='right')
dp1 = Label(first, text='Дальность полета 1 тела')
dp1.pack()
dp1o = Label(first, text='')
dp1o.pack()


button = Button(root, text="Сгенерировать", command=analysis)
button.pack(side='left')

root.mainloop()

关于python - python标签不实时更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53527382/

10-12 16:48