Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.41 KB | None | 0 0
  1. try:
  2.     import tkinter as tk
  3. except ImportError:
  4.     import Tkinter as tk
  5.  
  6. WIDTH, HEIGHT = 800, 600
  7. FONT_HEADER = ("Verdana", 16, "bold")
  8. FONT_OPTION = ('Verdana', 12)
  9.  
  10.  
  11. class MainApplication(tk.Frame):
  12.     def __init__(self, parent, *args, **kwargs):
  13.         tk.Frame.__init__(self, parent, *args, **kwargs)
  14.         self.parent = parent
  15.         self.parent.title('test')
  16.         self.parent.geometry(f'{WIDTH}x{HEIGHT}')
  17.         self.parent.minsize(WIDTH, HEIGHT)
  18.         self.parent.maxsize(WIDTH, HEIGHT)
  19.  
  20.         self.create_menu()
  21.         self.create_left_menu(10)
  22.         self.create_left_menu_options()
  23.  
  24.     def create_menu(self):
  25.         """Creates main menu"""
  26.         self.menu = tk.Menu(self)
  27.         self.menu_options = {}
  28.         self.parent.config(menu=self.menu)
  29.  
  30.         # FILE
  31.         self.menu_options['file'] = tk.Menu(self.menu, tearoff=0)
  32.         self.menu.add_cascade(label='File', menu=self.menu_options['file'])
  33.         self.menu_options['file'].add_command(label='New', command=lambda: about())
  34.         self.menu_options['file'].add_command(label='Open...', command=lambda: about())
  35.         self.menu_options['file'].add_separator()
  36.         self.menu_options['file'].add_command(label='Exit', command=lambda: self.parent.quit())
  37.  
  38.         # PRODUCTS
  39.         self.menu_options['products'] = tk.Menu(self.menu, tearoff=0)
  40.         self.menu.add_cascade(label='Products', menu=self.menu_options['products'])
  41.         self.menu_options['products'].add_command(label='WZ...', command=lambda: self.customize_left_menu('products', 'wz'))
  42.         self.menu_options['products'].add_command(label='PZ...', command=lambda: self.customize_left_menu('products', 'pz'))
  43.         self.menu_options['products'].add_command(label='About...', command=lambda: [about(), about()])
  44.  
  45.         # MAGAZYN
  46.         self.menu_options['magazyn'] = tk.Menu(self.menu, tearoff=0)
  47.         self.menu.add_cascade(label='Magazyn', menu=self.menu_options['magazyn'])
  48.         self.menu_options['magazyn'].add_command(label='Raporty...', command=lambda: self.customize_left_menu('magazyn', 'raporty'))
  49.         self.menu_options['magazyn'].add_command(label='Przepyw...', command=lambda: about())
  50.         self.menu_options['magazyn'].add_command(label='About...', command=lambda: about())
  51.  
  52.         # SETTINGS
  53.         self.menu_options['settings'] = tk.Menu(self.menu, tearoff=0)
  54.         self.menu.add_cascade(label='Setting', menu=self.menu_options['settings'])
  55.         self.menu_options['settings'].add_command(label='About...', command=lambda: about())
  56.  
  57.         # HELP
  58.         self.menu_options['help'] = tk.Menu(self.menu, tearoff=0)
  59.         self.menu.add_cascade(label='Help', menu=self.menu_options['help'])
  60.         self.menu_options['help'].add_command(label='About...', command=lambda: about())
  61.  
  62.     def create_left_menu(self, amount=6):
  63.         """Creates a dictionary self.left_menu which contains left menu.
  64.        
  65.        Args:
  66.            amount (int, optional, default=6): Amount of left menu elements
  67.        """
  68.         self.left_menu = {}
  69.  
  70.         self.left_menu['header'] = tk.Label(self.parent, text='Nagłówek opcji', font=FONT_HEADER)
  71.         for i in range(amount):
  72.             self.left_menu[f'button{i}'] = tk.Button(self.parent, text=f'Button{i}', font=FONT_OPTION, command=lambda: print('Hello'))
  73.  
  74.         self.hide_left_menu()
  75.  
  76.     def create_left_menu_options(self):
  77.         """Method to separate creation of left menu options from the rest of code. Creates self.left_menu_options and self.default_button_options"""
  78.         self.left_menu_options = {
  79.             'products': {
  80.                 'wz': {
  81.                     'header': 'Wydanie Zewnętrzne',
  82.                     'buttons': {
  83.                         'button0': {
  84.                             'text': 'Dodaj',
  85.                             'command': lambda: print('PRODUCTS > WZ > DODAJ'),
  86.                             'width': 15,
  87.                         },
  88.                         'button1': {
  89.                             'text': 'Zmień',
  90.                             'command': lambda: print('PRODUCTS > WZ > ZMIEŃ'),
  91.                             'width': 15,
  92.                         },
  93.                         'button2': {
  94.                             'text': 'Usuń',
  95.                             'command': lambda: print('PRODUCTS > WZ > USUŃ'),
  96.                             'width': 15,
  97.                         },
  98.                     }
  99.                 },
  100.                 'pz': {
  101.                     'header': 'Przyjęcie Zewnętrzne',
  102.                     'buttons': {
  103.                         'button0': {
  104.                             'text': 'Dodaj',
  105.                             'command': lambda: print('PRODUCTS > PZ > DODAJ'),
  106.                             'width': 15,
  107.                         },
  108.                         'button1': {
  109.                             'text': 'Zmień',
  110.                             'command': lambda: print('PRODUCTS > PZ > ZMIEŃ'),
  111.                             'width': 15,
  112.                         },
  113.                         'button2': {
  114.                             'text': 'Usuń',
  115.                             'command': lambda: print('PRODUCTS > PZ > USUŃ'),
  116.                             'width': 15,
  117.                         },
  118.                     }
  119.                 },
  120.             },
  121.             'magazyn': {
  122.                 'raporty': {
  123.                     'header': 'Raporty',
  124.                     'buttons': {
  125.                         'button0': {
  126.                             'text': 'Dodaj raport',
  127.                             'command': lambda: print('MAGAZYN > RAPORTY > DODAJ  RAPORTY'),
  128.                             'width': 15,
  129.                         },
  130.                         'button1': {
  131.                             'text': 'Edytuj raport',
  132.                             'command': lambda: print('MAGAZYN > RAPORTY > EDYTUJ RAPORT'),
  133.                             'width': 15,
  134.                         },
  135.                         'button2': {
  136.                             'text': 'Usuń raport',
  137.                             'command': lambda: print('MAGAZYN > RAPORTY > USUŃ RAPORT'),
  138.                             'width': 15,
  139.                         },
  140.                     }
  141.                 },
  142.             },
  143.         }
  144.  
  145.         # Create dictionary with default options for button
  146.         default_test_button = tk.Button()
  147.         self.default_button_options = {
  148.             'text': default_test_button['text'],
  149.             'command': default_test_button['command'],
  150.             'bg': default_test_button['bg'], # Background Color
  151.             'activebackground': default_test_button['activebackground'], # Background Color when under the cursor
  152.             'fg': default_test_button['fg'], # Font Color
  153.             'activeforeground': default_test_button['activeforeground'], # Background Color when under the cursor
  154.             'height': default_test_button['height'], # Height of button
  155.             'width': default_test_button['width'], # Width of button
  156.             'bd': default_test_button['bd'], # Borderwidth
  157.             'font': default_test_button['font'], # Font
  158.             'justify': default_test_button['justify'], # Justify to LEFT / CENTER / RIGHT
  159.             'padx': default_test_button['padx'], # Padding X
  160.             'pady': default_test_button['pady'], # Padding Y
  161.         }
  162.  
  163.     def place_left_menu(self, number=-1):
  164.         """Place left menu
  165.        
  166.        Args:
  167.            number (int, optional, default=-1): Number of left menu's elements to show
  168.  
  169.        number = -1        all elements
  170.        number = 0         no elements
  171.        """
  172.         # checking for valid input
  173.         number += 1
  174.         if number == 0 or number > len(self.left_menu):
  175.             number = len(self.left_menu)
  176.         elif number <= 1:
  177.             number = 0
  178.  
  179.         # placing buttons
  180.         for i, key in enumerate(list(self.left_menu)[:number]):
  181.             self.left_menu[key].place(x=5, y=50+40*i)
  182.  
  183.     def hide_left_menu(self):
  184.         """Hide left menu"""
  185.         for i, item in enumerate(self.left_menu):
  186.             self.left_menu[item].place_forget()
  187.  
  188.     def customize_left_menu(self, category, selected, hide=True, place=True):
  189.         """Change features of left_menu. Activates when menu option clicked.
  190.        
  191.        Args:
  192.            category (string): Menu's category (e.g File, About, Tools...)
  193.            selected (string): Option in menu's category (e.g New, Open, Browse...)
  194.            hide (bool, optional, default=True): Hide left menu
  195.            place (bool, optional, default=True): Place left menu
  196.        """
  197.         # Hide left menu
  198.         if hide:
  199.             self.hide_left_menu()
  200.  
  201.         option = self.left_menu_options[category][selected]
  202.  
  203.         # Change header's text
  204.         self.left_menu['header']['text'] = option['header']
  205.  
  206.         # Change button's features
  207.         for i, (_, button) in enumerate(option['buttons'].items()):
  208.             for feature in button:
  209.                 if button[feature]:
  210.                     self.left_menu[f'button{i}'][feature] = button[feature]
  211.                 else:
  212.                     self.left_menu[f'button{i}'][feature] = self.default_button_options[feature]
  213.  
  214.         # Place left menu
  215.         if place:
  216.             self.place_left_menu(len(option['buttons']))
  217.        
  218.  
  219. if __name__ == "__main__":
  220.     root = tk.Tk()
  221.     MainApplication(root)  # .pack(side="top", fill="both", expand=True)
  222.     root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement