2015-09-30 31 views
0

這是我的Python軟件的一部分來控制智能電視。我有功能「connection_status」。連接狀態功能給出錯誤並且不能正常工作

import fcntl, socket, struct 
import base64 
import time, datetime 
import netifaces 
from Tkinter import * 

root = Tk() 
root.title("Pepin's Samsung Smart TV Remote") 
root.geometry("391x595") #391 

class Application(): 
    """Pepin's Samsung Smart TV Remote""" 

    def __init__(self, master): 

     self.master = master 
     self.create_widgets() 

    def connection(self): 

     self.connection_status() 


    def connection_status(self): // this is the function that does not work right. 

     try: 

      connection_status = sock.recv(64) 
      print("Status: Connected") 
      self.label_connection_status['text'] = 'Status: Connected' 

     except socket.timeout: 

      connection_status = "" 
      print("Status: Disconnected") 
      self.label_connection_status['text'] = 'Status: Disconnected' 

     self.master.after(1000, self.connection_status, self.master) 

    def create_widgets(self): 
      btn_connect = Button(self.master, text = "CONNECT TO TV", width=19, height=2, command = lambda: self.connection()) 

app = Application(root) 
root.mainloop() 

但是當我調用該函數,它輸出我這個錯誤:

Status: Connected 
Exception in Tkinter callback 
Traceback (most recent call last): 
    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1535, in __call__ 
    return self.func(*args) 
    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 586, in callit 
    func(*args) 
TypeError: connection_status() takes exactly 1 argument (2 given) 

即使電視斷開,狀態保持「連接」!

+0

你是如何調用'connection_status'?該錯誤說你用一個額外的參數調用該函數,但該函數需要'self' – aschmid00

+0

@jonrsharpe否,我不給任何參數。 – PepinCZ

+0

@ aschmid00你,通過自我。添加到主帖。 – PepinCZ

回答

0

要調用self.connection_status這一行代碼:

self.master.after(1000, self.connection_status, self.master) 

這將導致以下函數調用:

self.connection_status(self.master) 

然而,connection_status不接受其他任何參數比self這是由python自動添加的。這就是爲什麼你得到錯誤connection_status() takes exactly 1 argument (2 given)self是第一個參數,而self.master是第二個參數,但該函數的設計僅接受self

幾乎沒有理由將對象屬性傳遞給同一對象的另一個方法,因爲該方法可以立即訪問這些屬性。所以,簡單的解決方案是從after調用刪除參數:

self.master.after(1000, self.connection_status)