Super Code!
Willkommen im Club der Golferbwbg hat geschrieben:code-golfing
Hier mein Skript ohne die eher "schmutzige" LC als Schleifenersatz. Habe mein Skript noch einmal überarbeitet aber ohne den Einsatz deiner try:... except: Variante:
Code: Alles auswählen
#!/usr/bin/env python3
import tkinter as tk
from functools import partial
class Application(object):
def __init__(self, root):
self.listbox = tk.Listbox(root, selectmode=tk.SINGLE)
self.listbox.insert(tk.END, *('Zeile {}'.format(i) for i in range(7)))
self.listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.listbox.select_set(0)
for button in ('nach oben', 'nach unten', 'Beenden'):
tk.Button(root, text=button, command=partial(self.callback, button)
).pack(side=tk.TOP, fill=tk.BOTH, expand=True)
def callback(self, button):
if button == 'Beenden': quit()
if button == 'nach oben': self.move_selection(-1)
if button == 'nach unten': self.move_selection(1)
def move_selection(self, displacement):
indices = self.listbox.curselection()
if not indices: return
index = indices[0]
if 0 <= index + displacement < self.listbox.size():
self.listbox.select_clear(index)
if displacement: self.listbox.select_set(index + displacement)
if not displacement: self.listbox.select_set(index + 1)
def main():
root = tk.Tk()
Application(root)
root.mainloop()
if __name__ == '__main__':
main()
Gruss wuf