-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.py
More file actions
58 lines (47 loc) · 1.87 KB
/
settings.py
File metadata and controls
58 lines (47 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import tkinter as tk
from tkinter import Button, Entry
import configparser
class SettingsWindow:
def __init__(self, master):
# Settings window parameters
self.master = master
self.master.config(background="#222831")
self.master.geometry("300x150")
self.master.minsize(300, 150)
self.master.title("Settings")
# Render labels/entry/button
self.label = tk.Label(master, text="Bind Key:", bg="#222831", fg="white")
self.label.pack(pady=10)
self.entry = Entry(master, bg="#31363F", fg="white", width=15)
self.entry.pack()
self.button = Button(master, text="Save", command=self.save_and_close)
self.button.pack(pady=10)
self.load_key()
def load_key(self):
# Load binded key in config.ini
try:
config = configparser.ConfigParser()
config.read("config.ini")
if "Settings" in config and "key" in config["Settings"]:
self.entry.insert(0, config["Settings"]["key"])
except FileNotFoundError:
# If the config file doesn't exist, create it using the default bind
self.create_default_config()
def save_and_close(self):
self.save_key()
self.master.destroy() # Close the settings window
def save_key(self):
key = self.entry.get()
config = configparser.ConfigParser()
config["Settings"] = {"key": key}
with open("config.ini", "w") as configfile:
config.write(configfile) # Save key in config.ini
def create_default_config(self):
config = configparser.ConfigParser()
config["Settings"] = {"key": "n"} # Set default key to 'n'
with open("config.ini", "w") as configfile:
config.write(configfile)
if __name__ == "__main__":
root = tk.Tk()
app = SettingsWindow(root)
root.mainloop()