在按钮

时间:2017-06-13 09:55:28

标签: python function button tkinter callback

我已收到有关此主题的帮助:comparing values from different dataframes line by line, python

我有一个代码,在我的tk inter程序中:

DF1 = pd.DataFrame({"X":[1,2,3,4,5,6],"Y":[1,2,3,4,5,6],"C":[12,22,33,45,13,56]})

DF2 = pd.DataFrame({"X":[1,5],"Y":[1,1],"X1":[5,1],"Y1":[5,5]})

def isInSquare(row, df2):
    c1 =  (row.X > df2.iloc[0].X) and (row.Y > df2.iloc[0].Y)
    c1 = c1 and  (row.X < df2.iloc[0].X1) and (row.Y < df2.iloc[0].Y1)
    c1 = c1 and (row.X < df2.iloc[1].X) and (row.Y > df2.iloc[1].Y)
    c1 = c1 and (row.X > df2.iloc[1].X1) and (row.Y < df2.iloc[1].Y1)
    return c1

    DF_NEW = DF1[DF1.apply(lambda x: isInSquare(x,DF2),axis = 1)]

    print DF_NEW

如何使用Tkinter中的按钮播放? 这不起作用:

if __name__ == '__main__':
    root = Tk()

    root.title('title')
    root.geometry("450x150+200+200")

    x = (root.winfo_screenwidth() - root.winfo_reqwidth()) / 2
    y = (root.winfo_screenheight() - root.winfo_reqheight()) / 2
    root.geometry("+%d+%d" % (x, y))

    b1 = Button(root, text='txt', font=('arial', 12), command=isInSquare(row, df2))
    b1.pack(side=LEFT, padx=5, pady=5)
    root.mainloop()

我收到的错误如下: NameError: name 'row' is not defined 或者我会离开command=isInSquare() 错误:TypeError: isInSquare() takes exactly 2 arguments (0 given) 有人可以帮忙吗? df1和df2是数据帧所以我不能把值放在命令中,我不知道该怎么做; /

感谢您的建议

1 个答案:

答案 0 :(得分:0)

这是因为你在初始化按钮时调用函数isInSquare,

b2 = Button(root, text='Convert', font=('arial', 12), command=isInSquare(row,df2))

你必须将一个函数传递给command参数,所以不要对你传递的函数使用任何括号

编辑:

如果您在初始化按钮时可以使用行和df2,则可以执行

b2 = Button(root, text='Convert', font=('arial', 12), command=lambda: isInSquare(row,df2))