02cf152ecb
i just hadn't noticed in the standalone script because the script did in fact end, but when called from pomodoroprompt.py itself, which keeps running, the window hung, froze, freezed, broke, was a problem. And i thought it was somehow because it was called from another file or program but really it was just this missing destroy that is somehow not in many examples.
75 lines
2.1 KiB
Python
75 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import gi
|
|
gi.require_version('Gtk', '3.0')
|
|
from gi.repository import Gtk, GLib
|
|
|
|
class PromptWindow(Gtk.Window):
|
|
|
|
def __init__(self, prompt=None, task=None):
|
|
|
|
if task is None:
|
|
task = ""
|
|
self.orig_task = task
|
|
|
|
Gtk.Window.__init__(self, title="Pomodoro Prompt")
|
|
self.set_size_request(500, 100)
|
|
self.set_border_width(10)
|
|
|
|
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
|
|
self.add(vbox)
|
|
|
|
self.entry = Gtk.Entry()
|
|
self.entry.set_text(self.orig_task)
|
|
self.entry.connect("activate", self.save)
|
|
|
|
vbox.pack_start(self.entry, True, True, 0)
|
|
|
|
hbox = Gtk.Box(spacing=6)
|
|
|
|
self.button_ok = Gtk.Button.new_with_label("OK")
|
|
self.button_ok.connect("clicked", self.save)
|
|
hbox.pack_start(self.button_ok, True, True, 0)
|
|
|
|
if task:
|
|
self.button_reset = Gtk.Button.new_with_label("Reset")
|
|
self.button_reset.connect("clicked", self.reset)
|
|
hbox.pack_start(self.button_reset, True, True, 0)
|
|
|
|
vbox.pack_start(hbox, True, True, 0)
|
|
|
|
if prompt:
|
|
self.set_title(prompt)
|
|
|
|
self.show_all()
|
|
|
|
def save(self, item):
|
|
# Note: item is the entry directly if enter is pressed, but the button
|
|
# (i guess) if the button is pressed, so we do not use it.
|
|
self.task = self.entry.get_text()
|
|
self.destroy()
|
|
|
|
def reset(self, item):
|
|
self.entry.set_text(self.orig_task)
|
|
|
|
def retrieve(self):
|
|
try:
|
|
return self.task
|
|
except AttributeError:
|
|
return None
|
|
# started to do conditional like this but maybe main program handles.
|
|
# else if self.orig_task:
|
|
# return "Pomodoro cancelled, original goal was:"
|
|
|
|
def prompt(prompt=None, task=None):
|
|
win = PromptWindow(prompt=prompt, task=task)
|
|
# This is needed to end the thread if the dialog is closed with the X.
|
|
win.connect("destroy", Gtk.main_quit)
|
|
win.show_all()
|
|
Gtk.main()
|
|
return win.retrieve()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
task = prompt(prompt="What'll you do next?", task="Something, probably!")
|
|
print(task)
|