2017-06-13 38 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