Code: Alles auswählen
#!/usr/bin/env python3
import urwid
class ButtonPressed(Exception):
def __init__(self, state):
self.state = state
def __str__(self):
return str(self.state)
def button_exit(button, state):
raise ButtonPressed(state)
def get_buttons(labels_to_action):
buttons = [
urwid.Button(label, func, state)
for label, (func, state) in labels_to_action.items()
]
width = max(map(len, labels_to_action)) + 4
return urwid.GridFlow(
buttons, width, 3, 1, "center"
)
def get_message_box(message, title="", confirm="OK", reject=None):
text = urwid.Text(message, "center")
button_actions = {confirm: (button_exit, True)}
if reject:
button_actions[reject] = (button_exit, False)
buttons = get_buttons(button_actions)
widgets = [urwid.Divider(), text, urwid.Divider(), buttons]
return urwid.LineBox(urwid.Pile(widgets), title)
def run_loop(box):
screen_widget = urwid.Filler(
urwid.Padding(box, "center", ("relative", 50))
)
urwid.MainLoop(screen_widget).run()
def main():
box = get_message_box(
"Geht das nicht noch etwas schöner?",
title="Frage", confirm="Ja", reject="Nein"
)
try:
run_loop(box)
except ButtonPressed as result:
print(result)
if __name__ == "__main__":
main()