import kivy
import kivy.properties as kyProps
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.config import Config
import time
import os
import shutil
from kivy.core.window import Window
from kivy.properties import NumericProperty,StringProperty
from kivy.uix.widget import Widget
from tkinter import filedialog
from model_api import load_model,predict_model_test,stop_model,predict_model
# ai model object
model = None
# loading kv language file
Builder.load_file('TechTutor.kv')
# disbaling touch screen emulation on mouse
Config.set("input","mouse","mouse,disable_multitouch")
class MyProgressBar(Widget):
set_value = NumericProperty(0)
# custom layout to hold all UI elements using a KV file for UI elements
class MyFloatLayout(FloatLayout):
progress_bar_value = NumericProperty(0)
error_file = StringProperty("")
# method for when start button is pressed
def start_press(self):
self.ids.progress_bar_background.set_value += 10
if(self.ids.progress_bar_background.set_value > 100):
self.ids.progress_bar_background.set_value = 0
self.progress_bar_value = self.ids.progress_bar_background.set_value
# global model
# method for when stop button is pressed
def stop_press(self):
stop_model()
# # method for when change grade key button is pressed
# def change_key_button(self):
# key_file = self.ids.grade_key_image.source
# # using tkinter to create a field dialog so it looks like default system
file explorer
# new_key_file = filedialog.askopenfile(initialdir="/",
# title = "Select a new grade key",
# filetypes=(("PNG","*.png"),
# ("JPG","*.jpg"),
# ("JPEG","*.jpeg"),
# ("HEIC","*.heic"),
# ("All Files","*.*")))
# if(new_key_file is None):
# pass
# elif not (new_key_file.name.endswith(('.png','.jpg','.jpeg','.heic'))):
# self.error_file = 'TechTutor only accepts .png .jpeg .jpg or .heic
files'
# else:
# self.error_file = ''
# key_file = new_key_file.name
# self.ids.grade_key_image.source = key_file
# method for when pause button is pressed
def pause_press(self):
print("here")
# Change Account Info
#==================================================================================
==
# Opens a file explorer to select a json file
# Saves the file to src/config_files and then saves the path to the config file
as well
def select_json_file(self):
# Open file dialog for JSON file selection
json_file = filedialog.askopenfile(
initialdir="/",
title="Select configuration file",
filetypes=(("JSON files", "*.json"), ("All Files", "*.*"))
)
if json_file is None:
return # User cancelled the file selection
elif not json_file.name.endswith('.json'):
self.error_file = 'Only .json files are accepted for configuration'
else:
self.error_file = ''
# Save the selected JSON file path to a configuration file
self.save_json_file(json_file.name)
# Save the JSON file to a designated folder and return its new path
def save_json_file(self, file_path):
config_dir = 'src/config_files'
os.makedirs(config_dir, exist_ok=True)
file_name = os.path.basename(file_path)
destination_path = os.path.join(config_dir, file_name)
try:
shutil.copy(file_path, destination_path)
print(f"JSON file saved to: {destination_path}")
self.save_config(destination_path)
except Exception as e:
print(f"Failed to save JSON file: {e}")
return None
# Save the path of the saved JSON file to json_config.txt
def save_config(self, file_path):
config_file_path = 'src/json_config.txt'
try:
with open(config_file_path, 'w') as config_file:
config_file.write(file_path)
print(f"Configuration file path saved to {config_file_path}:
{file_path}")
self.load_config()
except Exception as e:
print(f"Failed to save configuration file: {e}")
# Read and return the saved configuration path from json_config.txt
def load_config(self):
config_file_path = 'src/json_config.txt'
try:
with open(config_file_path, 'r') as config_file:
file_path = config_file.read().strip()
print(f"Loaded file path from config: {file_path}")
return file_path
except FileNotFoundError:
print("Configuration file not found.")
return None
# Method to save the sheet ID entered in the text box
def save_sheet_id(self):
sheet_id = self.ids.sheet_id_input.text
config_dir = 'src/config_files'
file_path = os.path.join(config_dir, 'sheet_id.txt')
# Ensure the config_files directory exists
os.makedirs(config_dir, exist_ok=True)
try:
with open(file_path, 'w') as file:
file.write(sheet_id)
self.error_file = "Sheet ID saved successfully!"
except Exception as e:
self.error_file = f"Error saving Sheet ID: {e}"
#==================================================================================
==
# main call loop for kivy to make application window
class TechTutorApp(App):
set_value = 5
def build(self):
# setting window background to white
Window.clearcolor = (49/255,51/255,56/255,1)
return MyFloatLayout()
# global model
# model = load_model("./model_test1.pt")
if __name__ == '__main__':
TechTutorApp().run()
<MyProgressBar>:
size: root.width, root.height
canvas.before:
Color:
rgba: 0.251, 0.259, 0.286, 1
Rectangle:
size: (self.width, self.height)
pos: (self.x, self.y)
canvas.after:
Color:
rgba: 0.71, 0.729, 0.757, 1
Rectangle:
id: blue_rect
size: (self.width * root.set_value / 100, self.height)
pos: (self.x, self.y)
<MyFloatLayout>:
FloatLayout:
# making size of layout the entire window
id: float_layout
size: root.width, root.height
# start button
Button:
id: start_button
size_hint: (0.25, 0.1)
pos_hint: {'center_x': 0.5, 'top': 0.2}
background_normal: 'ui_images/gray4.png'
background_down: 'ui_images/gray2.png'
text: "Start"
color: 0.710, 0.729, 0.757, 1
on_press: root.start_press()
# Account Button
Button:
id: account_button
size_hint: (0.25, 0.1)
pos_hint: {'center_x': 0.8, 'top': 0.2}
background_normal: 'ui_images/gray4.png'
background_down: 'ui_images/gray2.png'
text: "Change Account Credentials"
color: 0.710, 0.729, 0.757, 1
on_press: root.select_json_file()
# stop button
Button:
id: stop_button
size_hint: (0.25, 0.1)
pos_hint: {'center_x': 0.2, 'top': 0.2}
background_normal: 'ui_images/gray4.png'
background_down: 'ui_images/gray2.png'
text: "Stop"
color: 0.710, 0.729, 0.757, 1
on_press: root.stop_press()
MyProgressBar:
id: progress_bar_background
size_hint: (0.43, 0.03)
pos_hint: {'center_x': 0.5, 'top': 0.25} # Moved up to allow more
space below
Label:
id: percent_done_label
pos_hint: {'center_x': 0.5, 'top': 0.28} # Moved up
size_hint: (0.43, 0.03)
text: str(root.progress_bar_value) + '% Done'
font_size: 24
color: 1, 1, 1, 1
Label:
id: error_label
pos_hint: {'center_x': 0.5, 'top': 0.9} # Adjusted to move above the
white box area
size_hint: (0.8, 0.05)
text: root.error_file
font_size: 18
color: 0.6, 0, 0, 1
# Adding image
Image:
id: grade_key_image
source: './src/ui_images/logo.png'
pos_hint: {'center_x': 0.5, 'center_y': 0.6}
size_hint: (0.4, 0.4)
fit_mode: 'fill'
# Text input for sheet ID
TextInput:
id: sheet_id_input
size_hint: (0.25, 0.05)
pos_hint: {'center_x': 0.3, 'top': 0.08} # Moved slightly up
hint_text: "Enter Sheet ID"
multiline: False
font_size: 18
# Button to save sheet ID
Button:
id: save_sheet_button
text: "Save Sheet ID"
size_hint: (0.25, 0.05)
pos_hint: {'center_x': 0.7, 'top': 0.08} # Moved slightly up
on_press: root.save_sheet_id()