Created
October 13, 2022 21:26
-
-
Save ndonkoHenri/003cbc888c536ec6fdb7c3ae827ef7b7 to your computer and use it in GitHub Desktop.
qr-gen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from flet import * | |
from flet import icons, colors | |
import pyqrcode | |
def main(page: Page): | |
page.title = "QRcode Generator" | |
page.theme_mode = "light" | |
page.window_always_on_top = True | |
page.splash = ProgressBar(visible=False) | |
# page.vertical_alignment = "center" | |
page.horizontal_alignment = "center" | |
page.count = len(os.listdir("assets/generated-codes")) | |
def change_theme(e): | |
""" | |
Changes the app's theme_mode, from dark to light or light to dark. A splash(progress bar) is also shown. | |
:param e: The event that triggered the function | |
:type e: ControlEvent | |
""" | |
page.splash.visible = True | |
page.update() | |
page.theme_mode = "light" if page.theme_mode == "dark" else "dark" # changes the page's theme_mode | |
page.splash.visible = False | |
theme_icon_button.selected = not theme_icon_button.selected # changes the icon | |
time.sleep(1) # shows the progress bar for a second indicating that work is being done.. | |
page.update() | |
def generate_qr(e): | |
# global qr_count | |
try: | |
if qr_text.current.value.strip(): | |
page.splash.visible = True | |
page.update() | |
qr_code = pyqrcode.create(qr_text.current.value) | |
qr_code.svg(f"assets/generated-codes/output_{page.count}.svg", scale=10, debug=True) | |
# qr_image.current.src = os.listdir("assets/generated-codes")[-1] | |
qr_image.current.src = f"generated-codes/output_{page.count}.svg" | |
qr_image.current.update() | |
page.splash.visible = False | |
page.update() | |
print(qr_image.current.src) | |
# os.remove(f"assets/generated-codes/output_{page.count}.svg") | |
page.count += 1 | |
# page.count = len(os.listdir("assets/generated-codes")) + 1 | |
else: | |
page.show_snack_bar( | |
SnackBar( | |
Text("No Text was entered!"), | |
open=True | |
) | |
) | |
except Exception as e: | |
page.show_snack_bar( | |
SnackBar( | |
Text(f"An Error occured: {e}"), | |
open=True | |
) | |
) | |
def save(e): | |
if qr_text.current.value.strip(): | |
pass | |
else: | |
page.show_snack_bar( | |
SnackBar( | |
Text("No Text was entered!"), | |
open=True | |
) | |
) | |
theme_icon_button = IconButton( | |
icons.DARK_MODE, | |
selected=False, | |
selected_icon=icons.LIGHT_MODE, | |
icon_size=35, | |
tooltip="change theme", | |
on_click=change_theme, | |
style=ButtonStyle(color={"": colors.BLACK, "selected": colors.WHITE}, ), | |
) | |
page.appbar = AppBar( | |
title=Text( | |
"QRcode Generator", | |
color="white" | |
), | |
center_title=True, | |
bgcolor="blue", | |
actions=[theme_icon_button], | |
) | |
qr_image = Ref[Image]() | |
qr_text = Ref[TextField]() | |
# qr_count = 0 | |
page.add( | |
Column( | |
[ | |
TextField( | |
ref=qr_text, | |
max_length=250, | |
hint_text="enter text here..", | |
label="Text Field", | |
width=500, | |
height=90, | |
suffix=FilledButton("Generate", on_click=generate_qr), | |
on_submit=generate_qr, | |
), | |
Divider(), | |
Container(Image( | |
ref=qr_image, | |
src="i", | |
# scale=1, | |
width=370, | |
height=370, | |
# gapless_playback=True, | |
), alignment=Alignment(0, 0), ), | |
] | |
) | |
) | |
flet.app(target=main, assets_dir="assets") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment