-
Notifications
You must be signed in to change notification settings - Fork 0
/
style_container.py
158 lines (130 loc) · 7.44 KB
/
style_container.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# TODO:
# A. Depending on order of definition/calculation, may be possible to simply set numFrameStyles based on
# len(HypnicGUI.frames).
# 1. This would require storing a reference to the HypnicGUI instance as a member variable
# within this StyleContainer class.
# 2. It would also increase the importance of ensuring that EVERY frame ends up in the HypnicGUI.frames list
# a. Detrimental in some ways since this could lead to bugs
# b. At the same time, could be good in serving as a visual indicator of whether some frames are missing
# from the frames list (whether due to never having been added, or somehow having been manipulated erroneously)
__name__ = "style_container"
# Library Imports
from timeit import default_timer
import random
import tkinter
from tkinter import ttk
# Local Imports
import hypnic_helpers
# G L O B A L V A R I A B L E S
# DEFAULT STYLE PARAMETERS
DEFAULT_BG_COLOR = ""
class StyleContainer():
# Constructor has a gui_ parameter which is saved in self.gui in order to access the HypnicGUI members
def __init__(self, gui_):
# M E M B E R V A R I A B L E S
# INITIALIZATION TIMER
# Timer beginning upon initialization of this object
self.initTimer = default_timer()
# OTHER TIMERS
# List of all timers, initialized as containing only self.initTimer
self.timers = [self.initTimer]
# So that the StyleContainer instance can refer to the HypnicGUI instance
self.gui = gui_
# TODO: Remove this block comment; it's a relic from when DimensionContainer was initialized within this class
"""
# This class holds the DimensionContainer object instance, "dims",
# in order to access things like padding specification variables. It is initialized here
self.dims = dimension_container.DimensionContainer(self.gui)
"""
# This class also holds the ttk Style object, which can be referenced from the HypnicGUI instance if needed
self.ttkStyleObj = ttk.Style()
# Sets the ttk theme
# TODO: Change this, I'm only making it Win-XP because I think that'll make it stand out whether it's having an effect
#print(self.ttkStyleObj.theme_names())
#print(self.ttkStyleObj.theme_use())
self.ttkStyleObj.theme_use("xpnative")
# A list of strings which are the names of custom styles
# Any time a custom style is defined with the ttk.style.map() method, it should be stored
self.styles = []
# A list of strings which specifically refer to ttk Frame-related styles
self.frameStyles = []
# A list of strings which specifically refer to ttk Label-related styles
self.labelStyles = []
# A list of strings which specifically refer to styles for ttk Labels with an associated image
self.imageLabelStyles = []
# A list of strings which specifically refer to styles for ttk BUTTON widgets
self.buttonStyles = []
# A list of strings which specifically refer to styles for ttk Labels with associated text
self.textLabelStyles = []
# A list of strings which specifically refer to styles for ttk Labels with associated textvariable values
self.textvariableLabelStyles = []
# Defines and configures the GUI's ttk styles
self.ttkStyleConfig()
def ttkStyleConfig(self):
# D E F A U L T S T Y L E S
# These styles will apply to all relevant ttk widgets which are not given a custom style
# TButton represents the default ttk Button (the class name for ttk::button is TButton)
self.ttkStyleObj.configure("TButton",
padding=self.gui.dims.internalPaddingButton,
relief="flat",
background="#555")
# TFrame : ttk.Frame
self.ttkStyleObj.configure("TFrame",
background="#16B")
# TLabelFrame : ttk.labelFrame
self.ttkStyleObj.configure("TLabelFrame",
background="#5AF")
# TLabel : ttk.label
self.ttkStyleObj.configure("TLabel",
background="#9D3")
# C U S T O M S T Y L E S
# FRAME STYLES
# Creates an assortment of Frame styles with random background colors to keep grid elements visually distinct
# Number of random ttk Frame styles to generate
# TODO: Define this elsewhere, and based on hard-coded parameters
numFrameStyles = 50
for i in range(numFrameStyles):
# styleName values are "fs0.TFrame", "fs1.TFrame", etc
styleName = ("fs" + str(i) + ".TFrame")
# Configures a style object, with key styleName, using different colors generated by hypnic_helpers.py
# Frames are colored differently to make them more easily distinguished
if i % 5 == 0:
self.ttkStyleObj.configure(styleName, background=hypnic_helpers.rgbToHex(hypnic_helpers.getRandomRed()))
if i % 5 == 1:
self.ttkStyleObj.configure(styleName, background=hypnic_helpers.rgbToHex(hypnic_helpers.getRandomGray()))
if i % 5 == 2:
self.ttkStyleObj.configure(styleName, background=hypnic_helpers.rgbToHex(hypnic_helpers.getRandomPurple()))
if i % 5 == 3:
self.ttkStyleObj.configure(styleName, background=hypnic_helpers.rgbToHex(hypnic_helpers.getRandomGreen()))
if i % 5 == 4:
self.ttkStyleObj.configure(styleName, background=hypnic_helpers.rgbToHex(hypnic_helpers.getRandomBlue()))
# Adds styleName to self.frameStyles for future reference
self.frameStyles.append(styleName)
# COMBOBOX STYLES
# NOTE: Custom combobox style to save space when dealing with single-character Comboboxes
# TODO: DETERMINE WHY THIS ISN'T EFFECTING TCOMBOBOXes AND FIX IT
self.ttkStyleObj.configure("TCombobox", postoffset=(0, 0, 20, 0), background="#37A")
# SET / GET METHODS
# Good practice for use when modifying StyleContainer variables from externally (like from the HypnicGUI instance)
# Also good for clarity when pulling/calculating values from here to the GUI instance
# Returns a random element from self.styles
def getRandomStyle(self):
return self.styles[random.randrange(0, len(self.styles))]
# Returns a random element from self.frameStyles
def getRandomFrameStyle(self):
return self.frameStyles[random.randrange(0, len(self.frameStyles))]
# Returns a random element from self.labelStyles
def getRandomLabelStyle(self):
return self.labelStyles[random.randrange(0, len(self.labelStyles))]
# Returns a random element from self.buttonStyles
def getRandomButtonStyle(self):
return self.buttonStyles[random.randrange(0, len(self.buttonStyles))]
# Returns a random element from self.imageLabelStyles
def getRandomImageLabelStyle(self):
return self.imageLabelStyles[random.randrange(0, len(self.imageLabelStyles))]
# Returns a random element from self.textLabelStyles
def getRandomTextLabelStyle(self):
return self.textLabelStyles[random.randrange(0, len(self.textLabelStyles))]
# Returns a random element from self.taxtvariableLabelStyles
def getRandomTextvariableLabelStyle(self):
return self.textvariableLabelStyles[random.randrange(0, len(self.textvariableLabelStyles))]