2017-06-13 71 views
0

我收到的帮助在本主题:comparing values from different dataframes line by line, python回叫功能按钮

,我有一个代码,在我的TK间方案:

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的数据帧,所以我不能把值的命令中,我不知道该怎么做;在咨询/

感谢

+0

您需要定义'row' _before_在'B2 ='线使用。 – Lafexlos

+0

如果他只是定义row和df2它会在按下按钮后崩溃 –

回答

0

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

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

你要传给命令参数的函数,所以不要使用任何括号与函数传递有

编辑:

,如果在您初始化按钮行和DF2将是可用的,你可以做

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

我不知道我是否理解正确,但是当我在'command = isInSquare'中给出没有任何括号时,我收到相同的错误:'TypeError:isInSquare( )只需要2个参数(0给出)' – Pawe

+0

你必须以不同的方式进行设计,我不知道row和df2是从哪里来的,所以我不能具体告诉你,但是command函数中的函数不应该带任何参数,def isInSquare():...不会崩溃。是的,你必须知道这些参数row和df来自哪里。 –

+0

我编辑了我的问题,也用'lambda:'选项同样的错误 – Pawe