这是我控制智能电视的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 :(得分: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)