How to
Copy text to clipboard
All text elements support copy operation:
1from pyegui import *
2
3text = Str("many lines of text\nmany lines of text\nmany lines of text")
4textoneline = Str("one line of text")
5
6def update_func(ctx):
7 text_edit_multiline(text, hint_text="hint")
8 text_edit_singleline(textoneline, hint_text="hint")
9
10if __name__ == "__main__":
11 run_native("Hello World App", update_func)
12
However you can copy text programmatically:
1from pyegui import *
2
3# This item is to big to be inserted into clipboard
4# text_to_copy = "your mom"
5
6text_to_copy = "smol item"
7
8def update_func(ctx):
9 if button_clicked("Copy"):
10 ctx.copy_text(text_to_copy)
11
12if __name__ == "__main__":
13 run_native("Hello World App", update_func)
14
Open URL in a browser
Links are opened when clicked on. Also you can open them using ctx.open_url()
1from pyegui import *
2
3
4def update_func(ctx):
5 hyperlink_to("pyeguion GitHub", "https://github.com/GachiLord/pyegui")
6
7 if button_clicked("Open url"):
8 ctx.open_url("https://github.com")
9
10if __name__ == "__main__":
11 run_native("Hello World App", update_func)
12
Display non-latin and non-cyrillic characters
Download ttf font file and distribute it with your app
1from pyegui import *
2
3def update_func(ctx):
4 # provide absolute or relative path to the file
5 ctx.set_font("NotoSansJP-VariableFont_wght.ttf")
6 # do cool stuff with Japanese fonts
7 heading("天気の子")
8
9if __name__ == "__main__":
10 run_native("fonts", update_func)
11
Set theme
By default system’s theme is used. Theme is changed using these methods:
ctx.set_light_theme()
ctx.set_dark_theme()
ctx.set_system_theme()
Example app:
1from pyegui import *
2
3DARK = 0
4LIGHT = 1
5
6theme = Int(DARK)
7
8def get_theme_index(ctx):
9 if ctx.is_dark_theme:
10 return DARK
11 return LIGHT
12
13def set_theme(ctx, idx):
14 if idx == DARK:
15 ctx.set_dark_theme()
16 else:
17 ctx.set_light_theme()
18
19def update_func(ctx):
20 theme.value = get_theme_index(ctx)
21
22 radio_value(theme, DARK, "dark")
23 radio_value(theme, LIGHT, "light")
24
25 set_theme(ctx, theme.value)
26
27if __name__ == "__main__":
28 run_native("themes", update_func)
Center elements vertically
1from pyegui import *
2
3def update_func(ctx):
4 with Layout(LayoutType.HorizontalCentered):
5 heading("I'm horizontal")
6
7if __name__ == "__main__":
8 run_native("Hello World App", update_func)
9
Center elements horizontally
1from pyegui import *
2
3def update_func(ctx):
4 with Layout(LayoutType.VerticalCentered):
5 heading("I'm horizontal")
6
7if __name__ == "__main__":
8 run_native("Hello World App", update_func)
9
Center elements vertically and horizontally
You can do that for one element:
with Layout(LayoutType.CenteredAndJustified):
heading("Perfect center")
For more elements, read why you can’t:
https://users.rust-lang.org/t/help-with-alignment-in-eframe-egui/133702