关于python:TypeError:schreiben_methode()缺少1个必需的位置参数:’self’

TypeError: schreiben_methode() missing 1 required positional argument: 'self'

本问题已经有最佳答案,请猛点这里访问。

我对python非常陌生,现在我有了第一个真正的问题:我想写一个电子邮件程序,但我不能用这个类来发送邮件。我看到了很多关于这些错误的答案,但没有一个对我有用。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from tkinter import *

class Versenden(object):
    def testen(self):
        print("Funktioniert")


class Schreiben(object):


    def __init__(self, sender, smtpserver, smtppasswort):
        self.Absender = sender
        self.Smtpserver = smtpserver
        self.Smtppasswort = smtppasswort
        Schreiben.schreiben_methode()

    def schreiben_methode(self):
        root1 = Tk()
        versenden = Versenden()
        labelEmpfaenger = Label(root1, text="Empfaenger:")
        labelBetreff = Label(root1, text="Betreff:")
        empfaenger = Entry(root1)
        betreff = Entry(root1)
        inhalt=Text(root1, height=10, width=50)
        buttonSenden = Button(root1, text="senden", command=lambda: versenden.testen())

        labelEmpfaenger.grid(row=0, column=0)
        empfaenger.grid(row=0, column=1)
        labelBetreff.grid(row=1, column=0)
        betreff.grid(row=1, column=1)
        inhalt.grid(row=2, columnspan=2)
        buttonSenden.grid(row=3, columnspan=2)

        root1.mainloop()

root = Tk()
schreiben = Schreiben("a","a","a")

labelEmail = Label(root, text="Ihre Email-Addresse")
labelPasswort = Label(root, text="Ihr Passwort")
labelServer = Label(root, text="SMTP-Server")


email = Entry(root)
passwort = Entry(root)
server = Entry(root)

buttonWeiter = Button(root, text="Weiter", command=lambda: schreiben.schreiben_methode(email, server, passwort))


labelEmail.grid(row=0)
labelPasswort.grid(row=1)
email.grid(row=0, column=1)
passwort.grid(row=1, column=1)
labelServer.grid(row=2)
server.grid(row=2, column=1)
buttonWeiter.grid(row=3, columnspan=2)

root.mainloop()

PS:对不起,我英语不好,我是一个德国学生,所以所有变量和类的名称都用德语写,希望这不是问题:)


您应该在self上调用该方法,而不是直接从类中调用。

1
2
3
4
5
def __init__(self, sender, smtpserver, smtppasswort):
    self.Absender = sender
    self.Smtpserver = smtpserver
    self.Smtppasswort = smtppasswort
    self.schreiben_methode()