UI: add Utils.messagebox

automatically uses either new kvui.MessageBox or tkinter.messagebox
This commit is contained in:
black-sliver 2022-06-04 17:02:02 +02:00 committed by Fabian Dill
parent a2fc3d5b71
commit 103f9e0b85
2 changed files with 47 additions and 0 deletions

View File

@ -519,3 +519,30 @@ def get_fuzzy_results(input_word: str, wordlist: typing.Sequence[str], limit: ty
reverse=True)[0:limit] reverse=True)[0:limit]
) )
) )
def messagebox(title: str, text: str, error: bool = False) -> None:
def is_kivy_running():
if 'kivy' in sys.modules:
from kivy.app import App
return App.get_running_app() is not None
return False
if is_kivy_running():
from kvui import MessageBox
MessageBox(title, text, error).open()
return
# fall back to tk
try:
import tkinter
from tkinter.messagebox import showerror, showinfo
except Exception as e:
logging.error('Could not load tkinter, which is likely not installed. '
f'This attempt was made because messagebox was used for "{title}".')
raise e
else:
root = tkinter.Tk()
root.withdraw()
showerror(title, text) if error else showinfo(title, text)
root.update()

20
kvui.py
View File

@ -38,6 +38,7 @@ from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview.layout import LayoutSelectionBehavior from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.animation import Animation from kivy.animation import Animation
from kivy.uix.popup import Popup
fade_in_animation = Animation(opacity=0, duration=0) + Animation(opacity=1, duration=0.25) fade_in_animation = Animation(opacity=0, duration=0) + Animation(opacity=1, duration=0.25)
@ -268,6 +269,25 @@ class ConnectBarTextInput(TextInput):
return super(ConnectBarTextInput, self).insert_text(s, from_undo=from_undo) return super(ConnectBarTextInput, self).insert_text(s, from_undo=from_undo)
class MessageBox(Popup):
class MessageBoxLabel(Label):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._label.refresh()
self.size = self._label.texture.size
if self.width + 50 > Window.width:
self.text_size[0] = Window.width - 50
self._label.refresh()
self.size = self._label.texture.size
def __init__(self, title, text, error=False, **kwargs):
label = MessageBox.MessageBoxLabel(text=text)
separator_color = [217 / 255, 129 / 255, 122 / 255, 1.] if error else [47 / 255., 167 / 255., 212 / 255, 1.]
super().__init__(title=title, content=label, size_hint=(None, None), width=max(100, int(label.width)+40),
separator_color=separator_color, **kwargs)
self.height += max(0, label.height - 18)
class GameManager(App): class GameManager(App):
logging_pairs = [ logging_pairs = [
("Client", "Archipelago"), ("Client", "Archipelago"),