如何从条目小部件获取数据

时间:2020-01-18 05:48:58

标签: python-3.x tkinter

我希望我的程序从ans条目小部件中获取信息,并在if和elif语句中使用它

def目标(): root1 = Tk()

first_p_x = random.randint(1, 50)
first_p_y = random.randint(1, 50)
second_p_x = random.randint(1, 50)
second_p_y = random.randint(1, 50)

Label(root1, text=f'what is the slope of these two points ( {first_p_x} , {first_p_y} ) ( {second_p_x} , {second_p_y} )').grid(row=0, column=0, sticky='e')
ans = Entry(root1, width=30).grid(row=0, column=1,
                            padx=2, pady=2, sticky='we', columnspan=9)

delta_y = first_p_y - second_p_y
delta_x = first_p_x - second_p_x
a = Fraction(delta_y, delta_x)

if ans == 'help':
    Label(root1, text=f"so first you set up the problem as {first_p_y}-{second_p_y} over {first_p_x}-{second_p_x}").grid(row=2, column=0, sticky='e')
    Entry(root1, width=30).grid(row=2, column=1,
                            padx=2, pady=2, sticky='we', columnspan=9)

elif ans == a:
    Label(root1, text="Thats correct nice job").grid(row=1, column=0, sticky='e')

Button(root1, text="Submit", width=15, command=root1.destroy).grid(row=3, column=1, sticky='e', padx=2, pady=2)

1 个答案:

答案 0 :(得分:1)

您可以使用.get()方法,这是一个示例,您应该像下面的代码一样添加一个输入按钮

def ans_func():
    ans_data = ans.get()
    if ans_data == 'help'
        #your function
    elif:
        #your function

并在按钮上添加command ='ans_func'