TypeError: gassens() missing 1 required positional argument: 'self'
TypeError: gassens() missing 1 required positional argument: 'self'
此代码从 esp8266 获取气体传感器(使用“RequestHandler_httpd(BaseHTTPRequestHandler)”)
我希望它在接收到气体传感器时显示在键上(在“class HelloWorld -> def gassens”上)
但得到这个错误:
文件“panel2.py”,第 124 行,在 do_GET 中
HelloWorld.gassens()
类型错误:gassens() 缺少 1 个必需的位置参数:'self'
我是 python 的初学者,请帮忙
发送
from tkinter import *
from tkinter.ttk import *
import requests
from http.server import BaseHTTPRequestHandler, HTTPServer
from multiprocessing import Process
import time
gas = 0
hum = ''
g = 0
TheRequest = None
roomp=''
lump = 0
class HelloWorld:
global lump
global gas
def __init__(self, master):
frame = Frame(master)
frame.pack()
frame.columnconfigure( 0, pad = 2)
frame.columnconfigure( 1, pad = 2)
frame.rowconfigure( 0, pad = 2)
frame.rowconfigure( 1, pad = 2)
frame.rowconfigure( 2, pad = 2)
self.button = Button(
frame, text="Hello", command=self.button_pressed
)
#self.button.pack(side=LEFT, padx=5)
self.button.grid(row=0, column=0, pady=10, padx=10, sticky=W+E+N+S)
self.label = Label(frame, text="this is label")
#self.label.pack()
self.label.grid(row=0, column=1)
buttonStyle = Style()
buttonStyle.configure( "Normal.TButton",
background = "#91C497",
borderwidth = 1,
activeforeground = "#30903C",
compound = "BOTTOM")
buttonStyle.configure( "Selected.TButton",
background = "#107B1D",
borderwidth = 1,
activeforeground = "#30903C",
compound = "BOTTOM")
self.fanImage = PhotoImage(file="icons/ac.png")
self.addImage = PhotoImage(file="icons/add.png")
self.extractor_button = Button( frame,
text = "Extractor",
command = self.toggleFan,
image = self.fanImage,
style = "Normal.TButton")
#self.extractor_button.pack()
self.extractor_button.grid(row=1, column=1)
self.label2 = Label(frame, text="gas-sensor is here")
self.label2.grid(row=2, column=0, columnspan = 2)
def button_pressed(self):
TheRequest = requests.get('http://192.168.43.91/off')
def toggleFan(self):
global lump
if lump == 0 :
TheRequest = requests.get('http://192.168.43.91/on')
self.extractor_button.config(image=self.fanImage,style="Selected.TButton")
lump = 1
else :
TheRequest = requests.get('http://192.168.43.91/off')
self.extractor_button.config(image=self.fanImage,style="Normal.TButton")
lump = 0
#PROBLEM HERE
def gassens(self):
global gas
self.label2.config(text=gas)
class RequestHandler_httpd(BaseHTTPRequestHandler):
def do_GET(self):
global gas
global hum
global g
print('you got this massage')
print("request ::",self.requestline)
req = self.requestline
tstgas = req.find ("gas")
tsthum = req.find ("hum")
if tstgas == 5 :
gas = str()
gas = req.replace("GET /gas", "")
gas = gas.replace(" HTTP/1.1", "")
print("gaz :"+gas)
gas = int(gas)
#PROBLEM HERE
HelloWorld.gassens()
messagetosend = bytes('this is from pi',"utf")
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.send_header('Content-Length', len(messagetosend))
self.end_headers()
self.wfile.write(messagetosend)
return
def server_rp():
global gas
global g
try:
server_address_httpd = ('192.168.43.211',8080)
httpd = HTTPServer(server_address_httpd, RequestHandler_httpd)
print('server is started!')
httpd.serve_forever()
except:
print("khata dar server")
def main():
root = Tk()
root.geometry("250x150+300+300")
ex = HelloWorld(root)
root.mainloop()
if __name__ == '__main__':
p1 = Process(target=main)
p1.start()
p2 = Process(target=server_rp)
p2.start()
p1.join()
p2.join()
这不是您在 Python 中调用方法的方式。如果它是 Java 并且 gassens 是静态的,你可以这样做。但是,Python.
中没有静态变量的概念
HelloWorld.gassens()
相反,您想创建 class 的 实例 以使用函数 gassens。例如:
gas = HelloWorld(masterParam)
gas.gassens()
您不能在另一个进程中直接访问 tkinter 小部件。因此,在 HTTP 服务器部分使用 threading.Thread
而不是 multiprocessing.Process
,在主线程中使用 运行 tkinter GUI。
以下是根据您的代码提出的更改建议:
from threading import Thread
...
class HelloWorld:
...
def gassens(self, gas):
self.label2.config(text=gas)
class RequestHandler_httpd(BaseHTTPRequestHandler):
def do_GET(self):
# send back response
messagetosend = b'this is from pi'
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.send_header('Content-Length', len(messagetosend))
self.end_headers()
self.wfile.write(messagetosend)
req = self.requestline
tstgas = req.find('gas')
if tstgas == 5:
# requestline: GET /gasXXX HTTP/1.1
gas = req.split()[1][4:]
print('gaz:', gas)
ex.gassens(gas)
...
if __name__ == '__main__':
# run tkinter GUI in main thread
root = tk.Tk()
root.geometry('250x150+300+300')
ex = HelloWorld(root)
# using thread instead of process
Thread(target=server_rp, daemon=True).start()
root.mainloop()
此代码从 esp8266 获取气体传感器(使用“RequestHandler_httpd(BaseHTTPRequestHandler)”) 我希望它在接收到气体传感器时显示在键上(在“class HelloWorld -> def gassens”上) 但得到这个错误: 文件“panel2.py”,第 124 行,在 do_GET 中 HelloWorld.gassens() 类型错误:gassens() 缺少 1 个必需的位置参数:'self'
我是 python 的初学者,请帮忙 发送
from tkinter import *
from tkinter.ttk import *
import requests
from http.server import BaseHTTPRequestHandler, HTTPServer
from multiprocessing import Process
import time
gas = 0
hum = ''
g = 0
TheRequest = None
roomp=''
lump = 0
class HelloWorld:
global lump
global gas
def __init__(self, master):
frame = Frame(master)
frame.pack()
frame.columnconfigure( 0, pad = 2)
frame.columnconfigure( 1, pad = 2)
frame.rowconfigure( 0, pad = 2)
frame.rowconfigure( 1, pad = 2)
frame.rowconfigure( 2, pad = 2)
self.button = Button(
frame, text="Hello", command=self.button_pressed
)
#self.button.pack(side=LEFT, padx=5)
self.button.grid(row=0, column=0, pady=10, padx=10, sticky=W+E+N+S)
self.label = Label(frame, text="this is label")
#self.label.pack()
self.label.grid(row=0, column=1)
buttonStyle = Style()
buttonStyle.configure( "Normal.TButton",
background = "#91C497",
borderwidth = 1,
activeforeground = "#30903C",
compound = "BOTTOM")
buttonStyle.configure( "Selected.TButton",
background = "#107B1D",
borderwidth = 1,
activeforeground = "#30903C",
compound = "BOTTOM")
self.fanImage = PhotoImage(file="icons/ac.png")
self.addImage = PhotoImage(file="icons/add.png")
self.extractor_button = Button( frame,
text = "Extractor",
command = self.toggleFan,
image = self.fanImage,
style = "Normal.TButton")
#self.extractor_button.pack()
self.extractor_button.grid(row=1, column=1)
self.label2 = Label(frame, text="gas-sensor is here")
self.label2.grid(row=2, column=0, columnspan = 2)
def button_pressed(self):
TheRequest = requests.get('http://192.168.43.91/off')
def toggleFan(self):
global lump
if lump == 0 :
TheRequest = requests.get('http://192.168.43.91/on')
self.extractor_button.config(image=self.fanImage,style="Selected.TButton")
lump = 1
else :
TheRequest = requests.get('http://192.168.43.91/off')
self.extractor_button.config(image=self.fanImage,style="Normal.TButton")
lump = 0
#PROBLEM HERE
def gassens(self):
global gas
self.label2.config(text=gas)
class RequestHandler_httpd(BaseHTTPRequestHandler):
def do_GET(self):
global gas
global hum
global g
print('you got this massage')
print("request ::",self.requestline)
req = self.requestline
tstgas = req.find ("gas")
tsthum = req.find ("hum")
if tstgas == 5 :
gas = str()
gas = req.replace("GET /gas", "")
gas = gas.replace(" HTTP/1.1", "")
print("gaz :"+gas)
gas = int(gas)
#PROBLEM HERE
HelloWorld.gassens()
messagetosend = bytes('this is from pi',"utf")
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.send_header('Content-Length', len(messagetosend))
self.end_headers()
self.wfile.write(messagetosend)
return
def server_rp():
global gas
global g
try:
server_address_httpd = ('192.168.43.211',8080)
httpd = HTTPServer(server_address_httpd, RequestHandler_httpd)
print('server is started!')
httpd.serve_forever()
except:
print("khata dar server")
def main():
root = Tk()
root.geometry("250x150+300+300")
ex = HelloWorld(root)
root.mainloop()
if __name__ == '__main__':
p1 = Process(target=main)
p1.start()
p2 = Process(target=server_rp)
p2.start()
p1.join()
p2.join()
这不是您在 Python 中调用方法的方式。如果它是 Java 并且 gassens 是静态的,你可以这样做。但是,Python.
中没有静态变量的概念HelloWorld.gassens()
相反,您想创建 class 的 实例 以使用函数 gassens。例如:
gas = HelloWorld(masterParam)
gas.gassens()
您不能在另一个进程中直接访问 tkinter 小部件。因此,在 HTTP 服务器部分使用 threading.Thread
而不是 multiprocessing.Process
,在主线程中使用 运行 tkinter GUI。
以下是根据您的代码提出的更改建议:
from threading import Thread
...
class HelloWorld:
...
def gassens(self, gas):
self.label2.config(text=gas)
class RequestHandler_httpd(BaseHTTPRequestHandler):
def do_GET(self):
# send back response
messagetosend = b'this is from pi'
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.send_header('Content-Length', len(messagetosend))
self.end_headers()
self.wfile.write(messagetosend)
req = self.requestline
tstgas = req.find('gas')
if tstgas == 5:
# requestline: GET /gasXXX HTTP/1.1
gas = req.split()[1][4:]
print('gaz:', gas)
ex.gassens(gas)
...
if __name__ == '__main__':
# run tkinter GUI in main thread
root = tk.Tk()
root.geometry('250x150+300+300')
ex = HelloWorld(root)
# using thread instead of process
Thread(target=server_rp, daemon=True).start()
root.mainloop()