Gui.py (view raw)
1#!/usr/bin/python3
2import tkinter as tk
3from tkinter.filedialog import askopenfilename
4import pathlib
5import pygubu
6import Constants
7from Functions import check_rom, show_warning
8
9PROJECT_PATH = pathlib.Path(__file__).parent
10PROJECT_UI = PROJECT_PATH / "patcher.ui"
11
12class PatcherApp:
13 def __init__(self, master=None, baserom=None):
14 self.builder = builder = pygubu.Builder()
15 builder.add_resource_path(PROJECT_PATH)
16 builder.add_from_file(PROJECT_UI)
17 # Main widget
18 self.mainwindow = builder.get_object("main_frame", master)
19
20 self.browse_path = None
21 self.preset = None
22 self.font = None
23 self.places = None
24 self.sprites = None
25 self.palette = None
26 self.skip_m1 = None
27 self.progress = None
28 self.progress_text = None
29 builder.import_variables(
30 self,
31 [
32 "browse_path",
33 "preset",
34 "font",
35 "places",
36 "sprites",
37 "palette",
38 "skip_m1",
39 "progress",
40 "progress_text",
41 ],
42 )
43 self.font.set(1)
44 self.sprites.set(3)
45 self.places.set(6)
46 self.palette.set(8)
47 self.skip_m1.set(1)
48 self.progress_text.set(Constants.STATUS_START)
49
50 self.apply_button = builder.get_object("apply_button")
51
52 if baserom is not None:
53 self.browse_path.set(baserom)
54
55 builder.connect_callbacks(self)
56
57 def set_progress(self, percent, message):
58 self.progress.set(percent)
59 self.progress_text.set(message)
60
61
62
63 def run(self):
64 self.mainwindow.mainloop()
65
66 def on_browse_button(self):
67 fn = askopenfilename(filetypes=[(Constants.VAR_FILEPICKER, '*.gba')])
68 self.browse_path.set(fn)
69
70 def on_change_preset(self, option):
71 new_vars = Constants.PRESETS[option]
72 self.set_progress(0, Constants.STATUS_PRESET)
73
74 for key in new_vars.keys():
75 getattr(self, key).set(new_vars[key])
76
77 def on_apply_button(self):
78 self.apply_button['state'] = 'disabled'
79
80 if check_rom(self.browse_path.get()):
81 self.set_progress(20, Constants.STATUS_MD5)
82 else:
83 show_warning(Constants.WARNING_MD5_MISMATCH)
84 self.apply_button['state'] = 'normal'