Advertisement
here2share

# CTk_complex_example.py

May 13th, 2022
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.34 KB | None | 0 0
  1. # CTk_complex_example.py
  2.  
  3. import tkinter
  4. import tkinter.messagebox
  5. import customtkinter
  6.  
  7. customtkinter.set_appearance_mode("System")  # Modes: "System" (standard), "Dark", "Light"
  8. customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"
  9.  
  10.  
  11. class App(customtkinter.CTk):
  12.  
  13.     WIDTH = 780
  14.     HEIGHT = 520
  15.  
  16.     def __init__(self):
  17.         super().__init__()
  18.  
  19.         self.title("CustomTkinter complex example")
  20.         self.geometry(f"{App.WIDTH}x{App.HEIGHT}")
  21.         # self.minsize(App.WIDTH, App.HEIGHT)
  22.  
  23.         self.protocol("WM_DELETE_WINDOW", self.on_closing)  # call .on_closing() when app gets closed
  24.  
  25.         # ============ create two frames ============
  26.  
  27.         # configure grid layout (2x1)
  28.         self.grid_columnconfigure(1, weight=1)
  29.         self.grid_rowconfigure(0, weight=1)
  30.  
  31.         self.frame_left = customtkinter.CTkFrame(master=self,
  32.                                                  width=180,
  33.                                                  corner_radius=0)
  34.         self.frame_left.grid(row=0, column=0, sticky="nswe")
  35.  
  36.         self.frame_right = customtkinter.CTkFrame(master=self)
  37.         self.frame_right.grid(row=0, column=1, sticky="nswe", padx=20, pady=20)
  38.  
  39.         # ============ frame_left ============
  40.  
  41.         # configure grid layout (1x11)
  42.         self.frame_left.grid_rowconfigure(0, minsize=10)   # empty row with minsize as spacing
  43.         self.frame_left.grid_rowconfigure(5, weight=1)  # empty row as spacing
  44.         self.frame_left.grid_rowconfigure(8, minsize=20)    # empty row with minsize as spacing
  45.         self.frame_left.grid_rowconfigure(11, minsize=10)  # empty row with minsize as spacing
  46.  
  47.         self.label_1 = customtkinter.CTkLabel(master=self.frame_left,
  48.                                               text="CustomTkinter",
  49.                                               text_font=("Roboto Medium", -16))  # font name and size in px
  50.         self.label_1.grid(row=1, column=0, pady=10, padx=10)
  51.  
  52.         self.button_1 = customtkinter.CTkButton(master=self.frame_left,
  53.                                                 text="CTkButton 1",
  54.                                                 fg_color=("gray75", "gray30"),  # <- custom tuple-color
  55.                                                 command=self.button_event)
  56.         self.button_1.grid(row=2, column=0, pady=10, padx=20)
  57.  
  58.         self.button_2 = customtkinter.CTkButton(master=self.frame_left,
  59.                                                 text="CTkButton 2",
  60.                                                 fg_color=("gray75", "gray30"),  # <- custom tuple-color
  61.                                                 command=self.button_event)
  62.         self.button_2.grid(row=3, column=0, pady=10, padx=20)
  63.  
  64.         self.button_3 = customtkinter.CTkButton(master=self.frame_left,
  65.                                                 text="CTkButton 3",
  66.                                                 fg_color=("gray75", "gray30"),  # <- custom tuple-color
  67.                                                 command=self.button_event)
  68.         self.button_3.grid(row=4, column=0, pady=10, padx=20)
  69.  
  70.         self.switch_1 = customtkinter.CTkSwitch(master=self.frame_left)
  71.         self.switch_1.grid(row=9, column=0, pady=10, padx=20, sticky="w")
  72.  
  73.         self.switch_2 = customtkinter.CTkSwitch(master=self.frame_left,
  74.                                                 text="Dark Mode",
  75.                                                 command=self.change_mode)
  76.         self.switch_2.grid(row=10, column=0, pady=10, padx=20, sticky="w")
  77.  
  78.         # ============ frame_right ============
  79.  
  80.         # configure grid layout (3x7)
  81.         self.frame_right.rowconfigure((0, 1, 2, 3), weight=1)
  82.         self.frame_right.rowconfigure(7, weight=10)
  83.         self.frame_right.columnconfigure((0, 1), weight=1)
  84.         self.frame_right.columnconfigure(2, weight=0)
  85.  
  86.         self.frame_info = customtkinter.CTkFrame(master=self.frame_right)
  87.         self.frame_info.grid(row=0, column=0, columnspan=2, rowspan=4, pady=20, padx=20, sticky="nsew")
  88.  
  89.         # ============ frame_info ============
  90.  
  91.         # configure grid layout (1x1)
  92.         self.frame_info.rowconfigure(0, weight=1)
  93.         self.frame_info.columnconfigure(0, weight=1)
  94.  
  95.         self.label_info_1 = customtkinter.CTkLabel(master=self.frame_info,
  96.                                                    text="CTkLabel: Lorem ipsum dolor sit,\n" +
  97.                                                         "amet consetetur sadipscing elitr,\n" +
  98.                                                         "sed diam nonumy eirmod tempor" ,
  99.                                                    height=100,
  100.                                                    fg_color=("white", "gray38"),  # <- custom tuple-color
  101.                                                    justify=tkinter.LEFT)
  102.         self.label_info_1.grid(column=0, row=0, sticky="nwe", padx=15, pady=15)
  103.  
  104.         self.progressbar = customtkinter.CTkProgressBar(master=self.frame_info)
  105.         self.progressbar.grid(row=1, column=0, sticky="ew", padx=15, pady=15)
  106.  
  107.         # ============ frame_right ============
  108.  
  109.         self.radio_var = tkinter.IntVar(value=0)
  110.  
  111.         self.label_radio_group = customtkinter.CTkLabel(master=self.frame_right,
  112.                                                         text="CTkRadioButton Group:")
  113.         self.label_radio_group.grid(row=0, column=2, columnspan=1, pady=20, padx=10, sticky="")
  114.  
  115.         self.radio_button_1 = customtkinter.CTkRadioButton(master=self.frame_right,
  116.                                                            variable=self.radio_var,
  117.                                                            value=0)
  118.         self.radio_button_1.grid(row=1, column=2, pady=10, padx=20, sticky="n")
  119.  
  120.         self.radio_button_2 = customtkinter.CTkRadioButton(master=self.frame_right,
  121.                                                            variable=self.radio_var,
  122.                                                            value=1)
  123.         self.radio_button_2.grid(row=2, column=2, pady=10, padx=20, sticky="n")
  124.  
  125.         self.radio_button_3 = customtkinter.CTkRadioButton(master=self.frame_right,
  126.                                                            variable=self.radio_var,
  127.                                                            value=2)
  128.         self.radio_button_3.grid(row=3, column=2, pady=10, padx=20, sticky="n")
  129.  
  130.         self.slider_1 = customtkinter.CTkSlider(master=self.frame_right,
  131.                                                 from_=0,
  132.                                                 to=1,
  133.                                                 number_of_steps=3,
  134.                                                 command=self.progressbar.set)
  135.         self.slider_1.grid(row=4, column=0, columnspan=2, pady=10, padx=20, sticky="we")
  136.  
  137.         self.slider_2 = customtkinter.CTkSlider(master=self.frame_right,
  138.                                                 command=self.progressbar.set)
  139.         self.slider_2.grid(row=5, column=0, columnspan=2, pady=10, padx=20, sticky="we")
  140.  
  141.         self.slider_button_1 = customtkinter.CTkButton(master=self.frame_right,
  142.                                                        height=25,
  143.                                                        text="CTkButton",
  144.                                                        command=self.button_event)
  145.         self.slider_button_1.grid(row=4, column=2, columnspan=1, pady=10, padx=20, sticky="we")
  146.  
  147.         self.slider_button_2 = customtkinter.CTkButton(master=self.frame_right,
  148.                                                        height=25,
  149.                                                        text="CTkButton",
  150.                                                        command=self.button_event)
  151.         self.slider_button_2.grid(row=5, column=2, columnspan=1, pady=10, padx=20, sticky="we")
  152.  
  153.         self.checkbox_button_1 = customtkinter.CTkButton(master=self.frame_right,
  154.                                                          height=25,
  155.                                                          text="CTkButton",
  156.                                                          border_width=3,   # <- custom border_width
  157.                                                          fg_color=None,   # <- no fg_color
  158.                                                          command=self.button_event)
  159.         self.checkbox_button_1.grid(row=6, column=2, columnspan=1, pady=10, padx=20, sticky="we")
  160.  
  161.         self.check_box_1 = customtkinter.CTkCheckBox(master=self.frame_right,
  162.                                                      text="CTkCheckBox")
  163.         self.check_box_1.grid(row=6, column=0, pady=10, padx=20, sticky="w")
  164.  
  165.         self.check_box_2 = customtkinter.CTkCheckBox(master=self.frame_right,
  166.                                                      text="CTkCheckBox")
  167.         self.check_box_2.grid(row=6, column=1, pady=10, padx=20, sticky="w")
  168.  
  169.         self.entry = customtkinter.CTkEntry(master=self.frame_right,
  170.                                             width=120,
  171.                                             placeholder_text="CTkEntry")
  172.         self.entry.grid(row=8, column=0, columnspan=2, pady=20, padx=20, sticky="we")
  173.  
  174.         self.button_5 = customtkinter.CTkButton(master=self.frame_right,
  175.                                                 text="CTkButton",
  176.                                                 command=self.button_event)
  177.         self.button_5.grid(row=8, column=2, columnspan=1, pady=20, padx=20, sticky="we")
  178.  
  179.         # set default values
  180.         self.radio_button_1.select()
  181.         self.switch_2.select()
  182.         self.slider_1.set(0.2)
  183.         self.slider_2.set(0.7)
  184.         self.progressbar.set(0.5)
  185.         self.slider_button_1.configure(state=tkinter.DISABLED, text="Disabled Button")
  186.         self.radio_button_3.configure(state=tkinter.DISABLED)
  187.         self.check_box_1.configure(state=tkinter.DISABLED, text="CheckBox disabled")
  188.         self.check_box_2.select()
  189.  
  190.     def button_event(self):
  191.         print("Button pressed")
  192.  
  193.     def change_mode(self):
  194.         if self.switch_2.get() == 1:
  195.             customtkinter.set_appearance_mode("dark")
  196.         else:
  197.             customtkinter.set_appearance_mode("light")
  198.  
  199.     def on_closing(self, event=0):
  200.         self.destroy()
  201.  
  202.     def start(self):
  203.         self.mainloop()
  204.  
  205.  
  206. if __name__ == "__main__":
  207.     app = App()
  208.     app.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement