Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- import tkinter as tk
- except ImportError:
- import Tkinter as tk
- WIDTH, HEIGHT = 800, 600
- FONT_HEADER = ("Verdana", 16, "bold")
- FONT_OPTION = ('Verdana', 12)
- class MainApplication(tk.Frame):
- def __init__(self, parent, *args, **kwargs):
- tk.Frame.__init__(self, parent, *args, **kwargs)
- self.parent = parent
- self.parent.title('test')
- self.parent.geometry(f'{WIDTH}x{HEIGHT}')
- self.parent.minsize(WIDTH, HEIGHT)
- self.parent.maxsize(WIDTH, HEIGHT)
- self.create_menu()
- self.create_left_menu(10)
- self.create_left_menu_options()
- def create_menu(self):
- """Creates main menu"""
- self.menu = tk.Menu(self)
- self.menu_options = {}
- self.parent.config(menu=self.menu)
- # FILE
- self.menu_options['file'] = tk.Menu(self.menu, tearoff=0)
- self.menu.add_cascade(label='File', menu=self.menu_options['file'])
- self.menu_options['file'].add_command(label='New', command=lambda: about())
- self.menu_options['file'].add_command(label='Open...', command=lambda: about())
- self.menu_options['file'].add_separator()
- self.menu_options['file'].add_command(label='Exit', command=lambda: self.parent.quit())
- # PRODUCTS
- self.menu_options['products'] = tk.Menu(self.menu, tearoff=0)
- self.menu.add_cascade(label='Products', menu=self.menu_options['products'])
- self.menu_options['products'].add_command(label='WZ...', command=lambda: self.customize_left_menu('products', 'wz'))
- self.menu_options['products'].add_command(label='PZ...', command=lambda: self.customize_left_menu('products', 'pz'))
- self.menu_options['products'].add_command(label='About...', command=lambda: [about(), about()])
- # MAGAZYN
- self.menu_options['magazyn'] = tk.Menu(self.menu, tearoff=0)
- self.menu.add_cascade(label='Magazyn', menu=self.menu_options['magazyn'])
- self.menu_options['magazyn'].add_command(label='Raporty...', command=lambda: self.customize_left_menu('magazyn', 'raporty'))
- self.menu_options['magazyn'].add_command(label='Przepyw...', command=lambda: about())
- self.menu_options['magazyn'].add_command(label='About...', command=lambda: about())
- # SETTINGS
- self.menu_options['settings'] = tk.Menu(self.menu, tearoff=0)
- self.menu.add_cascade(label='Setting', menu=self.menu_options['settings'])
- self.menu_options['settings'].add_command(label='About...', command=lambda: about())
- # HELP
- self.menu_options['help'] = tk.Menu(self.menu, tearoff=0)
- self.menu.add_cascade(label='Help', menu=self.menu_options['help'])
- self.menu_options['help'].add_command(label='About...', command=lambda: about())
- def create_left_menu(self, amount=6):
- """Creates a dictionary self.left_menu which contains left menu.
- Args:
- amount (int, optional, default=6): Amount of left menu elements
- """
- self.left_menu = {}
- self.left_menu['header'] = tk.Label(self.parent, text='Nagłówek opcji', font=FONT_HEADER)
- for i in range(amount):
- self.left_menu[f'button{i}'] = tk.Button(self.parent, text=f'Button{i}', font=FONT_OPTION, command=lambda: print('Hello'))
- self.hide_left_menu()
- def create_left_menu_options(self):
- """Method to separate creation of left menu options from the rest of code. Creates self.left_menu_options and self.default_button_options"""
- self.left_menu_options = {
- 'products': {
- 'wz': {
- 'header': 'Wydanie Zewnętrzne',
- 'buttons': {
- 'button0': {
- 'text': 'Dodaj',
- 'command': lambda: print('PRODUCTS > WZ > DODAJ'),
- 'width': 15,
- },
- 'button1': {
- 'text': 'Zmień',
- 'command': lambda: print('PRODUCTS > WZ > ZMIEŃ'),
- 'width': 15,
- },
- 'button2': {
- 'text': 'Usuń',
- 'command': lambda: print('PRODUCTS > WZ > USUŃ'),
- 'width': 15,
- },
- }
- },
- 'pz': {
- 'header': 'Przyjęcie Zewnętrzne',
- 'buttons': {
- 'button0': {
- 'text': 'Dodaj',
- 'command': lambda: print('PRODUCTS > PZ > DODAJ'),
- 'width': 15,
- },
- 'button1': {
- 'text': 'Zmień',
- 'command': lambda: print('PRODUCTS > PZ > ZMIEŃ'),
- 'width': 15,
- },
- 'button2': {
- 'text': 'Usuń',
- 'command': lambda: print('PRODUCTS > PZ > USUŃ'),
- 'width': 15,
- },
- }
- },
- },
- 'magazyn': {
- 'raporty': {
- 'header': 'Raporty',
- 'buttons': {
- 'button0': {
- 'text': 'Dodaj raport',
- 'command': lambda: print('MAGAZYN > RAPORTY > DODAJ RAPORTY'),
- 'width': 15,
- },
- 'button1': {
- 'text': 'Edytuj raport',
- 'command': lambda: print('MAGAZYN > RAPORTY > EDYTUJ RAPORT'),
- 'width': 15,
- },
- 'button2': {
- 'text': 'Usuń raport',
- 'command': lambda: print('MAGAZYN > RAPORTY > USUŃ RAPORT'),
- 'width': 15,
- },
- }
- },
- },
- }
- # Create dictionary with default options for button
- default_test_button = tk.Button()
- self.default_button_options = {
- 'text': default_test_button['text'],
- 'command': default_test_button['command'],
- 'bg': default_test_button['bg'], # Background Color
- 'activebackground': default_test_button['activebackground'], # Background Color when under the cursor
- 'fg': default_test_button['fg'], # Font Color
- 'activeforeground': default_test_button['activeforeground'], # Background Color when under the cursor
- 'height': default_test_button['height'], # Height of button
- 'width': default_test_button['width'], # Width of button
- 'bd': default_test_button['bd'], # Borderwidth
- 'font': default_test_button['font'], # Font
- 'justify': default_test_button['justify'], # Justify to LEFT / CENTER / RIGHT
- 'padx': default_test_button['padx'], # Padding X
- 'pady': default_test_button['pady'], # Padding Y
- }
- def place_left_menu(self, number=-1):
- """Place left menu
- Args:
- number (int, optional, default=-1): Number of left menu's elements to show
- number = -1 all elements
- number = 0 no elements
- """
- # checking for valid input
- number += 1
- if number == 0 or number > len(self.left_menu):
- number = len(self.left_menu)
- elif number <= 1:
- number = 0
- # placing buttons
- for i, key in enumerate(list(self.left_menu)[:number]):
- self.left_menu[key].place(x=5, y=50+40*i)
- def hide_left_menu(self):
- """Hide left menu"""
- for i, item in enumerate(self.left_menu):
- self.left_menu[item].place_forget()
- def customize_left_menu(self, category, selected, hide=True, place=True):
- """Change features of left_menu. Activates when menu option clicked.
- Args:
- category (string): Menu's category (e.g File, About, Tools...)
- selected (string): Option in menu's category (e.g New, Open, Browse...)
- hide (bool, optional, default=True): Hide left menu
- place (bool, optional, default=True): Place left menu
- """
- # Hide left menu
- if hide:
- self.hide_left_menu()
- option = self.left_menu_options[category][selected]
- # Change header's text
- self.left_menu['header']['text'] = option['header']
- # Change button's features
- for i, (_, button) in enumerate(option['buttons'].items()):
- for feature in button:
- if button[feature]:
- self.left_menu[f'button{i}'][feature] = button[feature]
- else:
- self.left_menu[f'button{i}'][feature] = self.default_button_options[feature]
- # Place left menu
- if place:
- self.place_left_menu(len(option['buttons']))
- if __name__ == "__main__":
- root = tk.Tk()
- MainApplication(root) # .pack(side="top", fill="both", expand=True)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement