Created
January 1, 2021 04:45
-
-
Save danricho/2c9b328a331acf975b7314534b5a3542 to your computer and use it in GitHub Desktop.
Simple wrapper for some of TKinter
This file contains hidden or 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
from tkinter import * | |
from tkinter import ttk | |
from datetime import datetime | |
# A SMALL PYTHON CLASS I WROTE TO SIMPLIFY GUI DEVELOPMENT. | |
def myPrinter(str): | |
print(datetime.now().strftime('%H:%M:%S ') + str) | |
class Window: | |
def __init__(self, name="", size=[100,200], position=[100,200]): | |
self.name = name | |
self.running = False | |
self.size = size | |
self.position = position | |
self.margin_size = 10 | |
self.bottom_limit = 80 | |
self.column_width = 100 | |
self.tabs = {} | |
self.elements = {} | |
self.root = Tk() | |
self.root.protocol("WM_DELETE_WINDOW", self.stop) # close cross | |
self.root.bind("<Control-c>", self.stop) # keyboard interrupt | |
self.style = ttk.Style() | |
self.style.configure("TNotebook.Tab", font=('B612',10,'normal')) | |
self.style.configure("TNotebook.Tab", padding=[10,2]) | |
self.tabControl = ttk.Notebook(self.root, padding=self.margin_size/2, width=self.size[0], height=self.size[1]-self.bottom_limit) | |
self.root.title(name) | |
self.root.geometry(str(self.size[0]) + 'x' + str(self.size[1])) | |
self.root.geometry("+" + str(self.position[0]) + "+" + str(self.position[1])) | |
self.position_pointers = {} | |
self.position_pointers["root"] = [self.margin_size, self.margin_size] | |
def __repr__(self): | |
data = dict(vars(self)) | |
if "root" in data: | |
del data["root"] | |
return str({"tkinter": data}) | |
def __str__(self): | |
data = dict(vars(self)) | |
if "root" in data: | |
del data["root"] | |
return str({"tkinter": data}) | |
def is_running(): | |
self.running = True | |
def add_tab(self,title): | |
self.position_pointers[title] = [self.margin_size, self.margin_size] | |
self.tabs[title] = ttk.Frame(self.tabControl) | |
self.tabControl.add(self.tabs[title], text=title) | |
def get_initial_position(self): | |
return x_position,y_position | |
def add_y_margin(self, tab=None): | |
if not tab: | |
self.position_pointers["root"][1] += self.margin_size | |
else: | |
self.position_pointers[tab][1] += self.margin_size | |
def add_half_y_margin(self, tab=None): | |
if not tab: | |
self.position_pointers["root"][1] += self.margin_size / 2 | |
else: | |
self.position_pointers[tab][1] += self.margin_size / 2 | |
def add_indent(self, tab=None): | |
if not tab: | |
self.position_pointers["root"][0] += self.margin_size | |
else: | |
self.position_pointers[tab][0] += self.margin_size | |
def remove_indent(self, tab=None): | |
if not tab: | |
self.position_pointers["root"][0] -= self.margin_size | |
self.position_pointers["root"][0] = max(self.position_pointers["root"][0], self.margin_size) | |
else: | |
self.position_pointers[tab][0] -= self.margin_size | |
self.position_pointers[tab][0] = max(self.position_pointers[tab][0], self.margin_size) | |
def no_indent(self, tab=None): | |
if not tab: | |
self.position_pointers["root"][0] = self.margin_size | |
else: | |
self.position_pointers[tab][0] = self.margin_size | |
def add_heading(self, text, tab=None, x=None, y=None): | |
text = text.upper() | |
height = 22 | |
margin_bottom = 5 | |
if not x == None: | |
if x < 0: | |
x += self.size[0] | |
if not y == None: | |
if y < 0: | |
y += self.size[1] | |
if not tab: | |
parent = self.root | |
if not x: | |
x = self.position_pointers["root"][0] | |
if not y: | |
y = self.position_pointers["root"][1] | |
self.position_pointer[1] += height + margin_bottom | |
else: | |
parent = self.tabs[tab] | |
if not x: | |
x = self.position_pointers[tab][0] | |
if not y: | |
y = self.position_pointers[tab][1] | |
self.position_pointers[tab][1] += height + margin_bottom | |
self.elements["lab_" + text] = { "variable": None, "function": None, "label": None, | |
"tk_element": Label(parent, text=text, font=('B612',10,'bold'), anchor=NW, justify=LEFT), | |
} | |
self.elements["lab_" + text]["tk_element"].place(x=x, y=y, height=height) | |
def set_heading_text(self, oldtext, newtext): | |
if self.running: | |
newtext = newtext.upper() | |
oldtext = oldtext.upper() | |
if "lab_" + oldtext in self.elements: | |
self.elements["lab_" + newtext] = self.elements.pop("lab_" + oldtext) | |
self.elements["lab_" + newtext]["tk_element"].config(text=newtext) | |
def add_checkbox(self, label, ch_function, intial_state, tab=None, x=None, y=None): | |
height = 25 | |
margin_bottom = 0 | |
if not x == None: | |
if x < 0: | |
x += self.size[0] | |
if not y == None: | |
if y < 0: | |
y += self.size[1] | |
if not tab: | |
parent = self.root | |
if not x: | |
x = self.position_pointers["root"][0] | |
if not y: | |
y = self.position_pointers["root"][1] | |
self.position_pointers["root"][1] += height + margin_bottom | |
else: | |
parent = self.tabs[tab] | |
if not x: | |
x = self.position_pointers[tab][0] | |
if not y: | |
y = self.position_pointers[tab][1] | |
self.position_pointers[tab][1] += height + margin_bottom | |
self.elements["check_" + label] = { "label": None, | |
"variable": StringVar(), | |
"function": lambda: ch_function(label), | |
} | |
self.elements["check_" + label]["tk_element"] = Checkbutton( | |
parent, | |
text=label, | |
font=('B612',10,'normal'), | |
onvalue=True, | |
offvalue=False, | |
variable=self.elements["check_" + label]["variable"], | |
command=self.elements["check_" + label]["function"] | |
) | |
if intial_state: | |
self.elements["check_" + label]["tk_element"].select() | |
else: | |
self.elements["check_" + label]["tk_element"].deselect() | |
self.elements["check_" + label]["tk_element"].place(x=x, y=y, height=height) | |
def get_checkbox(self, ch_id): | |
if self.running: | |
if not ch_id.startswith("check_"): | |
ch_id = "check_" + ch_id | |
return bool(int(self.elements[ch_id]["variable"].get())) | |
def set_checkbox(self, label, state): | |
if self.running: | |
if state: | |
self.elements["check_" + label]["tk_element"].select() | |
else: | |
self.elements["check_" + label]["tk_element"].deselect() | |
def add_button(self, label, bt_function, tab=None, x=None, y=None): | |
height = 35 | |
margin_bottom = 0 | |
if not x == None: | |
if x < 0: | |
x += self.size[0] | |
if not y == None: | |
if y < 0: | |
y += self.size[1] | |
if not tab: | |
parent = self.root | |
if not x: | |
x = self.position_pointers["root"][0] | |
if not y: | |
y = self.position_pointers["root"][1] | |
self.position_pointers["root"][1] += height + margin_bottom | |
else: | |
parent = self.tabs[tab] | |
if not x: | |
x = self.position_pointers[tab][0] | |
if not y: | |
y = self.position_pointers[tab][1] | |
self.position_pointers[tab][1] += height + margin_bottom | |
self.elements["button_" + label] = { "variable": None, "label": None, | |
"function": bt_function, | |
} | |
self.elements["button_" + label]["tk_element"] = Button( | |
parent, | |
text=label, | |
font=('B612',10,'normal'), | |
command=bt_function, | |
padx=10 | |
) | |
self.elements["button_" + label]["tk_element"].place(x=x, y=y) | |
def add_spinbox(self, label, sp_function, intial_state, min, max, delta, tab=None, x=None, y=None): | |
label_height = 16 | |
spinner_height = 26 | |
margin_bottom = 0 | |
if not x == None: | |
if x < 0: | |
x += self.size[0] | |
if not y == None: | |
if y < 0: | |
y += self.size[1] | |
if not tab: | |
parent = self.root | |
if not x: | |
x = self.position_pointers["root"][0] | |
if not y: | |
y = self.position_pointers["root"][1] | |
self.position_pointers["root"][1] += label_height + spinner_height + margin_bottom | |
else: | |
parent = self.tabs[tab] | |
if not x: | |
x = self.position_pointers[tab][0] | |
if not y: | |
y = self.position_pointers[tab][1] | |
self.position_pointers[tab][1] += label_height + spinner_height + margin_bottom | |
self.elements["sp_" + label] = { | |
"variable": StringVar(), | |
"function": lambda: sp_function(label), | |
} | |
self.elements["sp_" + label]["label"] = Label(parent, | |
text=label, | |
font=('B612',10,'normal'), | |
anchor=W, | |
justify=LEFT) | |
self.elements["sp_" + label]["label"].place(x=x, y=y, height=label_height) | |
self.elements["sp_" + label]["tk_element"] = Spinbox(parent, | |
from_=min, to=max, increment=delta, | |
font=('B612',10,'normal'), | |
textvariable=self.elements["sp_" + label]["variable"], | |
command=self.elements["sp_" + label]["function"]) | |
self.elements["sp_" + label]["tk_element"].place(x=x, y=y+label_height, height=spinner_height) | |
self.elements["sp_" + label]["variable"].set(str(intial_state)) | |
def set_spinbox(self, label, state): | |
if self.running: | |
self.elements["sp_" + label]["variable"].set(str(state)) | |
def get_spinbox(self, ch_id): | |
if self.running: | |
if not ch_id.startswith("sp_"): | |
ch_id = "sp_" + ch_id | |
return int(self.elements[ch_id]["variable"].get()) | |
def add_label(self, id, label, color="black", tab=None, x=None, y=None, mono=False): | |
label_height = 18 | |
margin_bottom = 0 | |
if not x == None: | |
if x < 0: | |
x += self.size[0] | |
if not y == None: | |
if y < 0: | |
y += self.size[1] | |
if not tab: | |
parent = self.root | |
if not x: | |
x = self.position_pointers["root"][0] | |
if not y: | |
y = self.position_pointers["root"][1] | |
self.position_pointers["root"][1] += label_height + margin_bottom | |
else: | |
parent = self.tabs[tab] | |
if not x: | |
x = self.position_pointers[tab][0] | |
if not y: | |
y = self.position_pointers[tab][1] | |
self.position_pointers[tab][1] += label_height + margin_bottom | |
self.elements["label_" + id] = { | |
"variable": None, | |
"function": None, | |
} | |
if mono: | |
fontstyle = ('B612 Mono',12,'normal') | |
else: | |
fontstyle=('B612',10,'normal') | |
self.elements["label_" + id]["label"] = Label(parent, | |
text=label, | |
font=fontstyle, | |
anchor=W, | |
justify=LEFT, | |
fg=color) | |
self.elements["label_" + id]["label"].place(x=x, y=y, height=label_height) | |
def set_label(self, id, label, color="black"): | |
if self.running: | |
self.elements["label_" + id]["label"].config(text=label, fg=color) | |
else: | |
# print('not running yet') | |
pass | |
def start(self): | |
myPrinter("Tk_S Window".ljust(20)[0:16] + " : "+self.name+" started.") | |
try: | |
self.running = True | |
self.tabControl.pack() | |
self.root.mainloop() | |
except (KeyboardInterrupt, SystemExit): | |
self.running = False | |
self.stop() | |
def stop(self, *args): | |
try: | |
self.root.destroy() | |
self.running = False | |
myPrinter("Tk_S Window".ljust(20)[0:16] + " : "+self.name+" stopped.") | |
except: | |
pass |
This file contains hidden or 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 | |
os.system('cls') | |
import Tkinter_Simple | |
globalData = {} | |
def exitButton(*args): | |
globalData["tkinter"].stop() | |
def buttonFunction(*args): | |
globalData["tkinter"].set_heading_text("Button Heading", "Button Heading - CHANGED!!") | |
def checkBoxFunction(label, *args): | |
global globalData | |
if label in globalData: | |
old_value = globalData[label] | |
globalData[label] = globalData["tkinter"].get_checkbox(label) | |
globalData["tkinter"].set_heading_text("Checkbox Heading - " + str(old_value), "Checkbox Heading - " + str(globalData[label])) | |
else: | |
globalData[label] = globalData["tkinter"].get_checkbox(label) | |
globalData["tkinter"].set_heading_text("Checkbox Heading", "Checkbox Heading - " + str(globalData[label])) | |
def spinboxFunction(label, *args): | |
if label in globalData: | |
old_value = globalData[label] | |
globalData[label] = globalData["tkinter"].get_spinbox(label) | |
globalData["tkinter"].set_heading_text("Spinbox Heading - " + str(old_value), "Spinbox Heading - " + str(globalData[label])) | |
else: | |
globalData[label] = globalData["tkinter"].get_spinbox(label) | |
globalData["tkinter"].set_heading_text("Spinbox Heading", "Spinbox Heading - " + str(globalData[label])) | |
globalData["tkinter"] = Tkinter_Simple.Window(name="Dashboard Title", size=[500,600], position=[100,200]) | |
globalData["tkinter"].add_button("Exit", exitButton, tab=None, x=-61, y=-39) | |
globalData["tkinter"].add_tab("First Tab") | |
globalData["tkinter"].add_tab("Second Tab") | |
globalData["tkinter"].add_heading("first tab", tab="First Tab") | |
globalData["tkinter"].add_heading("Checkbox Heading", tab="First Tab") | |
globalData["tkinter"].add_checkbox("Checkbox 1", checkBoxFunction, False, tab="First Tab") | |
globalData["tkinter"].add_y_margin(tab="First Tab") | |
globalData["tkinter"].add_heading("second tab", tab="Second Tab") | |
globalData["tkinter"].add_heading("Button Heading", tab="Second Tab") | |
globalData["tkinter"].add_button("Change", buttonFunction, tab="Second Tab") | |
globalData["tkinter"].add_y_margin(tab="Second Tab") | |
globalData["tkinter"].add_heading("Spinbox Heading", tab="Second Tab") | |
globalData["tkinter"].add_spinbox("spinbox", spinboxFunction, 5, 1, 10, 1, tab="Second Tab") | |
globalData["tkinter"].add_y_margin(tab="Second Tab") | |
globalData["tkinter"].add_heading("Last Heading", tab="First Tab") | |
# print(globalData["tkinter"]) | |
globalData["tkinter"].start() | |
globalData["tkinter"].stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment