【发布时间】:2019-09-26 20:30:04
【问题描述】:
我做了一个补角计算器,计算两个角度是否为补或等于 90,我还使用 tkinter 在 GUI 上工作,其中有一个按钮应该执行一个函数,但它没有,我一直在努力,但我不明白它有什么问题。
像下面这样的Tkinter按钮的命令必须执行一个功能
process = Button(root, text="click!", command=function).pack()
但在我的代码中,它不起作用,我不知道为什么。请有人帮助我!!! 这是我的代码:
from tkinter import *
root = Tk()
root.title("Complement angles calculator")
root.geometry("400x400+0+0")
heading_app = Label(root, text="Complement angles calculator", font=("arial", 20), fg="steelblue").pack()
angle1 = Label(root, text="First angle : ", font=("arial", 20), fg="black").place(x=10, y=80)
fir_angle = IntVar()
angle1_box = Entry(root, width=15, textvariable=fir_angle, bg="#ffffff").place(x=185, y=92)
angle2 = Label(root, text="Second angle : ", font=("arial", 20), fg="black").place(x=10, y=120)
sec_angle = IntVar()
angle2_box = Entry(root, width=15, textvariable=sec_angle, bg="#ffffff").place(x=220, y=132)
angles = fir_angle.get() + sec_angle.get()
def function():
if angles == 90:
text = Label(root, text="Resault :", font=("arial", 15)).place(x=155, y=250)
text = Label(root, text="These are 90° Complement angles", font=("arial", 15)).place(x=50, y=300)
if angles == 180:
text = Label(root, text="Resault :", font=("arial", 15)).place(x=155, y=250)
text = Label(root, text="These are 180° Complement angles", font=("arial", 15)).place(x=50, y=300)
if angles == 360:
text = Label(root, text="Resault :", font=("arial", 15)).place(x=155, y=250)
text = Label(root, text="These are 360° Complement angles", font=("arial", 15)).place(x=50, y=300)
if angles != 90 or angles != 180 or angles != 360:
text = Label(root, text="Resault :", font=("arial", 15)).place(x=155, y=250)
text = Label(root, text="These are NOT Complement angles", font=("arial", 15)).place(x=40, y=300)
function()
process = Button(root, text="Are these Complement angles ?", font=("arial", 15), command=function).place(x=50, y=175)
root.mainloop()
为什么不执行 function() ? 感谢您帮助我!
【问题讨论】:
-
当我运行你的代码时,函数就会执行。为什么你认为它没有?你确定你的函数没有执行吗?还是它正在执行,只是没有按照您的预期执行?
-
function()的结果完全由angles决定,它是在您的程序启动期间计算的一次。因此,再次调用它不会改变屏幕上的任何内容。您需要将该计算移到函数中。
标签: python function button tkinter