Hacker Simulationsprogramm

Fragen zu Tkinter.
Antworten
python_freak
User
Beiträge: 16
Registriert: Sonntag 22. August 2010, 09:06
Wohnort: Mein Zimmer
Kontaktdaten:

Hallo, Ich bin neu bei Python und ich habe mir mal die Ideen hier angesehen, und habe dort Das "Hacker Simulationsprogramm" gesehen. Ich habe zu dieser Idee probiert selber ein Programm zu schreiben.

Bis jetzt sieht das so aus:

Code: Alles auswählen

# -*- coding: cp1252 -*-
import Tkinter as tk

pcs = {"192.168.10.20" : "123456789"} # IP-Adressen und Pc Codes

ipadresse = ""
code = ""

def eins(ipadresse):
    ipadresse = ipadresse+"1"
    return ipadresse

def zwei(ipadresse):
    ipadresse = ipadresse+"2"
    return ipadresse

def drei(ipadresse):
    ipadresse = ipadresse+"3"
    return ipadresse

def vier(ipadresse):
    ipadresse = ipadresse+"4"
    return ipadresse

def fuenf(ipadresse):
    ipadresse = ipadresse+"5"
    return ipadresse

def sechs(ipadresse):
    ipadresse = ipadresse+"6"
    return ipadresse

def sieben(ipadresse):
    ipadresse = ipadresse+"7"
    return ipadresse

def acht(ipadresse):
    ipadresse = ipadresse+"8"
    return ipadresse

def neun(ipadresse):
    ipadresse = ipadresse+"9"
    return ipadresse

def null(ipadresse):
    ipadresse = ipadresse+"0"
    return ipadresse

def punkt(ipadresse):
    ipadresse = ipadresse+"."
    return ipadresse

adresse = tk.Tk() # erstelle Tk-Fenster
adresse.title("IP-Adresse?")

#erstelle Buttons mit Zahlen für IP-Adresse
    
b1 = tk.Button(adresse, text = "1", command = lambda:eins(ipadresse))
b1.grid(row=0, column=0, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b2 = tk.Button(adresse, text = "2", command = lambda:zwei(ipadresse))
b2.grid(row=0, column=1, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b3 = tk.Button(adresse, text = "3", command = lambda:drei(ipadresse))
b3.grid(row=0, column=2, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b4 = tk.Button(adresse, text = "4", command = lambda:vier(ipadresse))
b4.grid(row=1, column=0, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b5 = tk.Button(adresse, text = "5", command = lambda:fuenf(ipadresse))
b5.grid(row=1, column=1, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b6 = tk.Button(adresse, text = "6", command = lambda:sechs(ipadresse))
b6.grid(row=1, column=2, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b7 = tk.Button(adresse, text = "7", command = lambda:sieben(ipadresse))
b7.grid(row=2, column=0, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b8 = tk.Button(adresse, text = "8", command = lambda:acht(ipadresse))
b8.grid(row=2, column=1, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b9 = tk.Button(adresse, text = "9", command = lambda:neun(ipadresse))
b9.grid(row=2, column=2, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b10 = tk.Button(adresse, text = "0", command = lambda:null(ipadresse))
b10.grid(row=3, column=1, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b11 = tk.Button(adresse, text = ".", command = lambda:punkt(ipadresse))
b11.grid(row=3, column=0, ipadx = 10, ipady = 5, padx = 2, pady = 2)
Wenn ich eine Zahl anklicke geht er zwar die entsprechende Funktion durch (z.B. eins()), aber wenn ich mir dann ansehe was in ipadresse steht, dann zeigt mir der PC an, dass dort nichts "eingetragen" wurde.
Irgendwie fehlt noch der Eintrag: ipadresse = eins(ipadresse) (z.B.), ich weiß aber nicht wohin damit.

Kann mir das jemand erklären oder sagen was ich falsch mache und wie ich den Fehler beheben kann?
Danke schon mal im voraus.

Gruss Benito (11 Jahre)
Zuletzt geändert von python_freak am Sonntag 22. August 2010, 11:27, insgesamt 1-mal geändert.
BlackJack

@python_freak: Der Name `ipadresse` ist lokal zu den Funktionen definiert. Jede der Funktionen hat eine lokale Variable mit dem Namen, die nur existiert solange der Funktionsaufruf abgearbeitet wird. Das ist eine andere Variable als das Modulglobale `ipadresse` was dadurch nicht verändert wird.

Ich denke bevor man nicht halbwegs mit objektorientierter Programmierung umgehen kann, sollte man sich noch nicht an GUI-Programme wagen.
python_freak
User
Beiträge: 16
Registriert: Sonntag 22. August 2010, 09:06
Wohnort: Mein Zimmer
Kontaktdaten:

Hallo BlackJack, vielen Dank für deine schnelle Antwort. Ich habe eine Lösung gefunden.
Ich habe in die Funktionen die Zeile global ipadresse eingefügt.
Das Programm sieht jetzt so aus:

Code: Alles auswählen

# -*- coding: cp1252 -*-
import Tkinter as tk

pcs = {"192.168.10.20" : "123456789"} # IP-Adressen und Pc Codes

ipadresse = ""
code = ""

def eins():
    global ipadresse
    x = ipadresse
    x = x+"1"
    ipadresse = x
    print x

def zwei():
    global ipadresse
    x = ipadresse
    x = x+"2"
    ipadresse = x
    print x

def drei():
    global ipadresse
    x = ipadresse
    x = x+"3"
    ipadresse = x
    print x

def vier():
    global ipadresse
    x = ipadresse
    x = x+"4"
    ipadresse = x
    print x

def fuenf():
    global ipadresse
    x = ipadresse
    x = x+"5"
    ipadresse = x
    print x

def sechs():
    global ipadresse
    x = ipadresse
    x = x+"6"
    ipadresse = x
    print x

def sieben():
    global ipadresse
    x = ipadresse
    x = x+"7"
    ipadresse = x
    print x

def acht():
    global ipadresse
    x = ipadresse
    x = x+"8"
    ipadresse = x
    print x

def neun():
    global ipadresse
    x = ipadresse
    x = x+"9"
    ipadresse = x
    print x
    
def null():
    global ipadresse
    x = ipadresse
    x = x+"0"
    ipadresse = x
    print x

def punkt():
    global ipadresse
    x = ipadresse
    x = x+"."
    ipadresse = x
    print x

adresse = tk.Tk() # erstelle Tk-Fenster
adresse.title("IP-Adresse?")

#erstelle Buttons mit Zahlen für IP-Adresse
    
b1 = tk.Button(adresse, text = "1", command = lambda:eins())
b1.grid(row=0, column=0, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b2 = tk.Button(adresse, text = "2", command = lambda:zwei())
b2.grid(row=0, column=1, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b3 = tk.Button(adresse, text = "3", command = lambda:drei())
b3.grid(row=0, column=2, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b4 = tk.Button(adresse, text = "4", command = lambda:vier())
b4.grid(row=1, column=0, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b5 = tk.Button(adresse, text = "5", command = lambda:fuenf())
b5.grid(row=1, column=1, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b6 = tk.Button(adresse, text = "6", command = lambda:sechs())
b6.grid(row=1, column=2, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b7 = tk.Button(adresse, text = "7", command = lambda:sieben())
b7.grid(row=2, column=0, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b8 = tk.Button(adresse, text = "8", command = lambda:acht())
b8.grid(row=2, column=1, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b9 = tk.Button(adresse, text = "9", command = lambda:neun())
b9.grid(row=2, column=2, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b10 = tk.Button(adresse, text = "0", command = lambda:null())
b10.grid(row=3, column=1, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b11 = tk.Button(adresse, text = ".", command = lambda:punkt())
b11.grid(row=3, column=0, ipadx = 10, ipady = 5, padx = 2, pady = 2)

adresse.mainloop()
Vielen Dank für deine Hilfe.

Gruss Benito
Benutzeravatar
DaMutz
User
Beiträge: 202
Registriert: Freitag 31. Oktober 2008, 17:25

global ist böse :evil: und sonst ist dein Programm auch reichlich kompliziert. Du brauchst nur ein Funktion für das erzeugen der IP Adresse und dann kannst du die Buttons 1 - 9 auch in einer Schleife erzeugen. Dadurch schrumpft dein Programm auf <30 Zeilen.

Damit werden die Buttons 1 - 9 erstellt. Die add Funktion hängt das jeweilige Zeichen dem String hinzu.

Code: Alles auswählen

for i in range(9):
        b = tk.Button(adresse, text = i+1, command = lambda k=i+1: add(str(k)))
        b.grid(row = i // 3, column = i % 3, ipadx = 10, ipady = 5, padx = 2, pady = 2)
python_freak
User
Beiträge: 16
Registriert: Sonntag 22. August 2010, 09:06
Wohnort: Mein Zimmer
Kontaktdaten:

Hallo DaMutz, auch dir Danke ich für Deine Antwort ich habe das Programm jetzt um einiges kürzer geschrieben. Ich blicke dort jetzt auch besser durch.

Hier mein aktuelles Programm:

Code: Alles auswählen

# -*- coding: cp1252 -*-
import Tkinter as tk

pcs = {"192.168.10.20" : "123456789"} # IP-Adressen und Pc Codes

ipadresse = ""
code = ""

def bPressed(x):
    global ipadresse
    y = ipadresse
    y = y+x
    print y
    ipadresse = y

adresse = tk.Tk() # erstelle Tk-Fenster
adresse.title("IP-Adresse?")

#erstelle Buttons mit Zahlen für IP-Adresse
    
for i in range(9):
    b = tk.Button(adresse, text = i+1, command = lambda k=i+1:bPressed(str(k)))
    b.grid(row = i // 3, column = i % 3, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b10 = tk.Button(adresse, text = "0", command = lambda:bPressed("0"))
b10.grid(row=3, column=1, ipadx = 10, ipady = 5, padx = 2, pady = 2)

b11 = tk.Button(adresse, text = ".", command = lambda:bPressed("."))
b11.grid(row=3, column=0, ipadx = 10, ipady = 5, padx = 2, pady = 2)

adresse.mainloop()
Vielen Dank weitere Verbesserungen nehme ich gerne an.

Gruss Benito
Antworten