我需要调用第二类内部的函数print3()
。但是我需要从类内部调用,但不能使用任何函数:
class two:
y = 2
def __init__(self):
print('Login starts')
self.dict ={'a':1, 'b':2, 'c':3, 'd':4}
def print2(self, y):
print(y)
print(self.dict)
def print3(self):
print('print3')
print3()
x = two()
x.print2(2)
我得到的错误是:
Traceback (most recent call last):
File "B:/data/loginMech/test 2.py", line 6, in <module>
class two:
File "B:/data/loginMech/test 2.py", line 22, in two
print3()
TypeError: print3() missing 1 required positional argument: 'self'
也不能将任何值作为self
传递。
实际上,它是针对具有以下条件的Web抄袭项目:
from tkinter import *
import mechanicalsoup, random, re, json
# from termcolor import colored
from colorama import Fore,init, Back
init()
class Mygui:
def window(self, colour='black'):
loginClass = login()
self.main_window=Tk()
self.main_window.geometry('300x100')
self.main_window.title('Login')
self.top_frame=Frame(self.main_window)
self.top_frame.pack()
self.label=Label(self.top_frame, fg=colour, text="Your Cont", width=45)
self.label.pack(side="top")
self.label1=Label(self.top_frame,text=" ", width=45)
self.label1.pack(side="top")
self.my_button = Button(self.main_window, text="Retry", command=loginClass, height=2, width=18)
self.my_button.pack()
mainloop()
def do_something(self):
print('ok')
class login:
def __init__(self):
print('Login starts')
fp = open('dict.json', 'r')
self.dict = json.load(fp)
# key, pas = random.choice(list(dict.items()))
# print(key, pas)
self.browser = mechanicalsoup.StatefulBrowser()
def webloading(self):
print('webloading starts')
key, pas = random.choice(list(self.dict.items()))
# print(key, pas)
try:
self.browser.open("http://172.16.16.16:8090/") # starting of login site
except:
print('No connection to Login Page')
exit()
self.browser.select_form('form[action="httpclient.html"]')
self.browser.select_form('form[action="httpclient.html"]') # form selection from the site
# a = input('user ')
# b = input('pass ')
self.browser['username'] = key # '1239'
self.browser['password'] = pas # ''
response = self.browser.submit_selected() # Response of login
# sText = response.text
msg = re.compile(r'<message><!\[CDATA\[(.*?)\]').findall(response.text)[0] # fetching login status using regex
return msg, key
print('Login Checking starts')
msg, key = webloading()
if msg == 'You have successfully logged in':
print(Back.YELLOW + Fore.BLUE + 'Logged in with ' + key)
else:
webloading()
M = Mygui()
M.window('blue')
对于这个错误,我得到的是:
Login Checking starts
Traceback (most recent call last):
File "B:/data/loginMech/pro2.py", line 26, in <module>
class login:
File "B:/data/loginMech/pro2.py", line 60, in login
msg, key = webloading()
TypeError: webloading() missing 1 required positional argument: 'self'
答案 0 :(得分:0)
def print3()只是一个声明。因此,当您在类内部调用print3()时,它不知道它是什么。您需要作为实例方法x.print3()调用,或者在其他任何类方法内部调用print3(),例如在print2()内部:
答案 1 :(得分:0)
更改
def print3(self):
print('print3')
收件人
def print3():
print('print3')
请注意,def prin3(self)
意味着如果方法是静态的,则需要由对象或类调用它。