Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[OT] Problem mit Python (n00b)
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum
View previous topic :: View next topic  
Author Message
Vortex375
Veteran
Veteran


Joined: 03 Mar 2005
Posts: 1739
Location: Deutschland

PostPosted: Sat Oct 08, 2005 4:03 pm    Post subject: [OT] Problem mit Python (n00b) Reply with quote

Hallo allerseits!

Ich habe vor ein paar Tagen angefangen mir Python beizubringen (Ich habe so gut wie keine Programmier Erfahrung, also seid ein wenig nachsichtig :wink: ). Bis jetzt lief auch alles, dank einiger echt guten Tutorials von python.org, sehr gut.

Nun wollte ich aber mal probieren ein kleines Testprogramm zu schreiben:
Ich erstelle ein Textfeld, zwei Buttons und eine Listbox mithilfe der Tkinter Bibliothek.
Ich definiere eine Funktion, die entweder den Inhalt des Textfeldes der Listbox hinzufügt oder den Inhalt der Listbox löscht, abhängig davon, welchen parameter man angibt.

Aber genau das funktioniert nicht. Die Funktion wird nicht definiert sonder direkt ausgeführt.
Direkt nach programmstart erscheint zweimal "Test", was zeigt, dass sogar das "if" ignoriert wird.

Ich habe schon alles versucht und auch den Namen der Funktion (im Moment "Hugozorizer") und die der Option ("Hugo") geändert.
Er erkennt einfach nicht an, dass er die Funktion definieren soll.

Hier das Programm (ist ja kurz genug hoff ich, ich habe extra noch comments eingefügt :P ):

Code:

#!/usr/bin/python

# import Tkinter library

from Tkinter import *

# define root window and the listbox and entry widget

root = Tk()

listbox = Listbox(root)
entry = Entry(root)


# define the hugozorizer function (DOES NOT WORK!!!!!!)

def hugozorizer(hugo):
        if hugo == "addtolist":
                print "test"
                content = entry.get()
                listbox.insert(END,content)
                entry.delete(0,END)
        elif hugo == "clearlist":
                print "test"
                listbox.delete(0,END)
        return

# define two buttons

addbutton = Button(root, text = "Add Entry to List", command = hugozorizer("addtolist"))
clearbutton = Button(root, text = "Clear the List", command = hugozorizer("clearlist"))

#pack all widgets

entry.pack()
addbutton.pack()
clearbutton.pack()
listbox.pack()

root.mainloop()


Hoffentlich gibt es jemanden, der sich mit Python auskennt und mir die Augen öffnen kann.

Es muss irgendein n00b Problem sein. Ich habe das Programm auch schonmal gäntlich neu geschrieben in einem anderen Editor aber es will einfach nicht. :(
Back to top
View user's profile Send private message
Vortex375
Veteran
Veteran


Joined: 03 Mar 2005
Posts: 1739
Location: Deutschland

PostPosted: Sat Oct 08, 2005 4:12 pm    Post subject: Reply with quote

Zum Vergleich:

Hier klappt es ohne Probleme, die Funktion wird definiert und erst ausgeführt wenn sie aufgerufen wird:
Code:

#!/usr/bin/python

def printsomething(hugo):
    if hugo == "fisch":
        print "Es ist ein Fisch"
    elif hugo == "Bratpfanne":
        print "Es ist eine Bratpfanne"

printsomething("fisch")
printsomething("Bratpfanne")

print "Und nochmal:"

printsomething("fisch")
printsomething("Bratpfanne")
Back to top
View user's profile Send private message
smg
Veteran
Veteran


Joined: 13 Aug 2004
Posts: 1402
Location: /home/stephan

PostPosted: Sat Oct 08, 2005 4:14 pm    Post subject: Reply with quote

Am besten liest du ein richtige Pythoneinführung auf python.org.
Zu deinem Problem: Die Funktion wird doch definiert..
Und guck mal unten bei deinen Buttons die hast du so gestaltet dass ja test ausgegeben wird, da die if abfrage true ist.
Probiere es doch mal in eine Klasse zu schreiben und dann
Code:
if __name__ == "__main__":
zu benutzen.

Cheers.
_________________
GnuPG-Key-ID: 0xF8C275D4
Fingerprint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
Code:
perl -WTe '($")=$/;print qq(@{[reverse('0'..'100')]}$/BOOM!$/);'
Back to top
View user's profile Send private message
Vortex375
Veteran
Veteran


Joined: 03 Mar 2005
Posts: 1739
Location: Deutschland

PostPosted: Sat Oct 08, 2005 4:22 pm    Post subject: Reply with quote

Das Problem ist, dass das "Test" direkt ausgegeben wird, wenn ich das Programm starte. Wenn ich einen der Buttons drücke passiert nichts.

Die Funktion wird also nicht definiert, sondern alles was unterhalb von "def" und "if" steht direkt ausgeführt.

Oder ist das nur bei mir so?? Kann mal einer das Programm auf seinem Rechner ausführen und bestätigen, dass es wirklich nicht geht?
Back to top
View user's profile Send private message
smg
Veteran
Veteran


Joined: 13 Aug 2004
Posts: 1402
Location: /home/stephan

PostPosted: Sat Oct 08, 2005 4:24 pm    Post subject: Reply with quote

Probiere doch eine Klasse zu schreiben und nicht den imperativen Kram da. :)

Cheers.
_________________
GnuPG-Key-ID: 0xF8C275D4
Fingerprint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
Code:
perl -WTe '($")=$/;print qq(@{[reverse('0'..'100')]}$/BOOM!$/);'
Back to top
View user's profile Send private message
Vortex375
Veteran
Veteran


Joined: 03 Mar 2005
Posts: 1739
Location: Deutschland

PostPosted: Sat Oct 08, 2005 4:31 pm    Post subject: Reply with quote

Schön, schön, Klassen hatte ich noch nicht. :D

Ich werd jetzt erstmal brav die Einführung weiterlesen.

Aber interessieren würde es mich trotzdem, warum es nicht funktioniert.
Back to top
View user's profile Send private message
Fauli
l33t
l33t


Joined: 24 Apr 2004
Posts: 760
Location: Moers, Germany

PostPosted: Sat Oct 08, 2005 7:54 pm    Post subject: Reply with quote

Die Funktion hugozorizer muss eine Funktion zurückliefern:
Code:
def hugozorizer(hugo):
    def f():
        if hugo == "addtolist":
            print "test"
            content = entry.get()
            listbox.insert(END,content)
            entry.delete(0,END)
        elif hugo == "clearlist":
            print "test"
            listbox.delete(0,END)
    return f

_________________
Do your part to beautify the web! Turn off link underlining!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum