Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from functools import partial
- from tkinter import (
- Tk, StringVar,
- )
- from tkinter.ttk import (
- Button, Label, Entry,
- )
- # Callback mit partial
- # pfunc = partial(ausgabe, 42)
- # pfunc()
- # Oder mitels lambda
- # f = lambda: ausgabe(variable)
- # f()
- def ausgabe(variable):
- print('Instanz:',variable, 'Typ:', type(variable))
- text = variable.get()
- if text:
- print('Text:', text)
- else:
- print('Entry is leer')
- def make_gui():
- root = Tk()
- root.title('Titel')
- variable = StringVar()
- Label(root, text='').pack()
- etr = Entry(root, textvariable=variable)
- etr.pack()
- Button(root, text='Übertragen (partial)', command=partial(ausgabe, variable)).pack()
- # wenn der Button gedrückt wird, wird commad ausgeführt
- # command übergibt der Funktion keine Argumente!
- # mit partial oder lambda lässt sich das aber erweitern
- Button(root, text='Übertragen (lambda)', command=lambda: ausgabe(variable)).pack()
- # und hier übergebe ich anstatt die Variable
- # die Instanz etr der Klasse Entry
- # da die Klasse Entry auch die Methode get hat, muss an dem Code
- # in der Ausgabefunktion auch keine Unterscheidung stattfinden
- Button(root, text='Übertragen (direkt)', command=lambda: ausgabe(etr)).pack()
- Button(root, text='Beenden', command=root.destroy).pack()
- return root
- if __name__ == '__main__':
- root = make_gui()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement