-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
main.py is an example file. CTkSlideView.py is the main file.
- Loading branch information
1 parent
2952240
commit b28c3ae
Showing
3 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
class CTkAnimator: | ||
def __init__(self, from_=0, to=100, type=None, acceleration=0.01): | ||
self.from_ = from_ | ||
self.to = to | ||
self.type = type | ||
self.t = 0 | ||
self.completed = False | ||
self.acceleration = acceleration | ||
def map_range(self, value, start1, stop1, start2, stop2): | ||
""" | ||
Maps a value from one range to another range. | ||
Parameters: | ||
value (float): The value to be mapped. | ||
start1 (float): The lower bound of the input range. | ||
stop1 (float): The upper bound of the input range. | ||
start2 (float): The lower bound of the output range. | ||
stop2 (float): The upper bound of the output range. | ||
Returns: | ||
float: The mapped value in the second range. | ||
""" | ||
return start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1)) | ||
|
||
def __ease_in_out_quart(self, x, output_start, output_end): | ||
if x < 0.5: | ||
return output_start + (output_end - output_start) * (8 * x ** 4) | ||
else: | ||
return output_start + (output_end - output_start) * (1 - (-2 * x + 2) ** 4 / 2) | ||
|
||
def get(self): | ||
if not self.completed: | ||
if self.t >= 1.0: | ||
|
||
self.completed = True | ||
|
||
self.t += self.acceleration | ||
|
||
if self.type == None: | ||
x = self.t | ||
return self.__ease_in_out_quart(x, self.from_, self.to) | ||
def change_val(self, from_=0, to=100, type=None, acceleration=0.01): | ||
self.from_ = from_ | ||
self.to = to | ||
self.type = type | ||
self.t = 0 | ||
|
||
self.completed = False | ||
self.acceleration = acceleration | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
from customtkinter import CTkLabel, CTkFrame, CTkButton, CENTER, DISABLED, NORMAL | ||
from CTkAnimator import CTkAnimator | ||
|
||
class CTkSlideView(CTkFrame): | ||
def __init__(self, *args, | ||
width: int = 900, | ||
height: int = 500, | ||
**kwargs): | ||
super().__init__(*args, width=width, height=height, **kwargs) | ||
self.w = width | ||
self.h = height | ||
self.tabs = [] | ||
self.main_smoother = self.w/2 | ||
self.guide = self.w/2 | ||
self.right_btn = CTkButton(self, text=">", width=40, height=40, bg_color="transparent", font=("SF Display", 20), command=self.move_left) | ||
self.right_btn.place(x=self.w-50, rely=0.5, anchor=CENTER) | ||
|
||
self.left_btn = CTkButton(self, text="<", width=40, height=40, bg_color="transparent", font=("SF Display", 20), command=self.move_right, state=DISABLED) | ||
self.left_btn.place(x=50, rely=0.5, anchor=CENTER) | ||
self.left_anim = CTkAnimator(from_=0, to=self.w, acceleration=0.01) | ||
self.right_anim = CTkAnimator(from_=0, to=-self.w, acceleration=0.01) | ||
self.indicator_frame = CTkFrame(self, height=25, corner_radius=0, fg_color="transparent") | ||
self.indicator_frame.place(relx=0.5, y=self.h-15, anchor=CENTER) | ||
|
||
|
||
|
||
def move_left(self): | ||
if int(self.tabs[-1][0].place_info()["x"]) == self.w/2: | ||
self.right_btn.configure(state=DISABLED) | ||
else: | ||
self.right_btn.configure(state=NORMAL) | ||
if int(self.tabs[0][0].place_info()["x"]) != self.w / 2: | ||
self.left_btn.configure(state=NORMAL) | ||
val = self.left_anim.get() | ||
|
||
for i, x in enumerate(self.tabs): | ||
|
||
x[0].place(x=x[1]-val, y=((self.h-20)/2)) | ||
|
||
if self.left_anim.completed != True: | ||
self.winfo_toplevel().after(10, self.move_left) | ||
|
||
else: | ||
self.left_anim.change_val(from_=self.left_anim.from_, to=self.left_anim.from_+self.w) | ||
for i, x in enumerate(self.tabs): | ||
#x[0].place(x=x[1] - val, rely=0.5) | ||
self.tabs[i][1] = int(self.tabs[i][0].place_info()["x"]) | ||
if self.tabs[i][1] == self.w/2: | ||
self.tabs[i][2].configure(fg_color="white") | ||
else: | ||
self.tabs[i][2].configure(fg_color="grey") | ||
|
||
|
||
def move_right(self): | ||
if int(self.tabs[0][0].place_info()["x"]) == self.w / 2: | ||
self.left_btn.configure(state=DISABLED) | ||
else: | ||
self.left_btn.configure(state=NORMAL) | ||
if int(self.tabs[-1][0].place_info()["x"]) != self.w/2: | ||
self.right_btn.configure(state=NORMAL) | ||
val = self.right_anim.get() | ||
for i, x in enumerate(self.tabs): | ||
x[0].place(x=x[1] - val, y=((self.h-20)/2)) | ||
|
||
|
||
if self.right_anim.completed != True: | ||
self.winfo_toplevel().after(10, self.move_right) | ||
|
||
else: | ||
self.right_anim.change_val(from_=self.right_anim.from_, to=self.right_anim.from_ - self.w) | ||
for i, x in enumerate(self.tabs): | ||
# x[0].place(x=x[1] - val, rely=0.5) | ||
self.tabs[i][1] = int(self.tabs[i][0].place_info()["x"]) | ||
if self.tabs[i][1] == self.w/2: | ||
self.tabs[i][2].configure(fg_color="white") | ||
else: | ||
self.tabs[i][2].configure(fg_color="grey") | ||
|
||
|
||
|
||
|
||
def create_tab(self): | ||
frame = CTkFrame(self, width=self.w-100, height=self.h-50) | ||
frame.place(x=self.guide, y=((self.h-20)/2), anchor=CENTER) | ||
lbl = CTkLabel(self.indicator_frame, width=5, height=5, fg_color="grey", corner_radius=8, text="", font=("SF Display", 1)) | ||
lbl.pack(side="left", padx=5) | ||
frame.pack_propagate(False) | ||
frame.grid_propagate(False) | ||
self.tabs.append([frame, self.guide, lbl]) | ||
self.tabs[0][2].configure(fg_color="white") | ||
self.left_btn.lift() | ||
self.right_btn.lift() | ||
self.guide += self.w | ||
return frame |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from customtkinter import * | ||
from CTkSlideView import CTkSlideView | ||
|
||
set_appearance_mode("dark") | ||
set_default_color_theme("dark-blue") | ||
|
||
|
||
root = CTk() | ||
root.geometry("1800x900") | ||
root.title("CTk SlideMenu") | ||
|
||
|
||
#An Example For Slide Menu | ||
|
||
slide_menu = CTkSlideView(root) | ||
slide_menu.place(relx=0.5, rely=0.5, anchor=CENTER) | ||
lbl = CTkLabel(slide_menu.create_tab(), text="Slide 1", font=("SF Display", 50)).place(relx=0.5, rely=0.5, anchor=CENTER) | ||
lbl2 = CTkLabel(slide_menu.create_tab(), text="Slide 2", font=("SF Display", 50)).place(relx=0.5, rely=0.5, anchor=CENTER) | ||
lbl3 = CTkLabel(slide_menu.create_tab(), text="Slide 3", font=("SF Display", 50)).place(relx=0.5, rely=0.5, anchor=CENTER) | ||
lbl4 = CTkLabel(slide_menu.create_tab(), text="Slide 4", font=("SF Display", 50)).place(relx=0.5, rely=0.5, anchor=CENTER) | ||
lbl5 = CTkLabel(slide_menu.create_tab(), text="Slide 5", font=("SF Display", 50)).place(relx=0.5, rely=0.5, anchor=CENTER) | ||
|
||
|
||
|
||
|
||
|
||
root.mainloop() |