首页 > 解决方案 > 类型错误:gassens() 缺少 1 个必需的位置参数:'self'

问题描述

此代码从 esp8266 获取气体传感器(使用“RequestHandler_httpd(BaseHTTPRequestHandler)”)我希望它在收到气体传感器时显示在键上(在“class HelloWorld -> def gassens”上)但收到此错误:文件“panel2 .py",第 124 行,在 do_GET HelloWorld.gassens() 中类型错误:gassens() 缺少 1 个必需的位置参数:'self'

我是python的初学者请帮助tnx

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()

标签: pythonfunctionclasstkintertk

解决方案


这不是您在 Python 中调用方法的方式。如果它是 Java 并且 gassens 是静态的,你可以这样做。但是,Python 中没有静态变量的概念。

HelloWorld.gassens()

相反,您想创建一个类的实例来使用函数 gassens。例如:

gas = HelloWorld(masterParam)
gas.gassens()

推荐阅读