Advertisement
ALEXANDAR_GEORGIEV

widget_factory

Feb 12th, 2023
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.71 KB | Source Code | 0 0
  1. import tkinter as tk
  2. import tkinter.ttk as ttk
  3.  
  4.  
  5. class PopMessage:
  6.     def __init__(self, parent, function_on_answer, text, number_buttons, text_color):
  7.         self.pop = tk.Toplevel(parent)
  8.         self.pop.title("Confirmation")
  9.         self.pop.geometry("500x200+600+300")
  10.         self.pop.config(bg="red")
  11.         self.pop.attributes('-topmost', 'true')  # TODO -> НЕ РАБОТИ !!!
  12.         self.pop.wm_overrideredirect(True)
  13.         frame = tk.Frame(self.pop)
  14.         frame.pack(fill='both', padx=2, pady=2, expand=True)
  15.         label_frame = tk.LabelFrame(frame, bg='lightgrey', text='избор', labelanchor='n', fg='blue',
  16.                                     font=('Times', 9, 'italic'))
  17.         label_frame.pack(fill='x', padx=1, pady=(1, 1), side='bottom')
  18.         # TODO -> Label
  19.         label = tk.Label(frame, text=text, fg=text_color, font=('Timesbd', 12, 'italic'))
  20.         label.pack(anchor='center', pady=30)
  21.         # TODO -> Buttons
  22.         if number_buttons == 2:
  23.             butt_yes = tk.Button(label_frame, text="ДА", fg='darkgreen', width=10, font=('Timesbd', 9), borderwidth=3,
  24.                                  command=lambda: self.give_answer_and_close('yes'))
  25.             butt_yes.pack(pady=20, side='left', anchor='center', padx=(150, 5))
  26.             butt_no = tk.Button(label_frame, text="НЕ", fg='red', font=('Timesbd', 9), borderwidth=3, width=10,
  27.                                 command=lambda: self.give_answer_and_close('no'))
  28.             butt_no.pack(pady=20, side='right', anchor='center', padx=(5, 150))
  29.         elif number_buttons == 1:
  30.             butt_ok = tk.Button(label_frame, text="OK", fg='black', font=('Timesbd', 9), borderwidth=3, width=10,
  31.                                 command=lambda: self.give_answer_and_close('ok'))
  32.             butt_ok.pack(pady=20, anchor='center')
  33.         self.function_on_answer = function_on_answer
  34.  
  35.     def give_answer_and_close(self, answer):
  36.         self.function_on_answer(answer)
  37.         self.pop.destroy()
  38.  
  39.  
  40. class WidgetsFactory:
  41.     def __init__(self):
  42.         self.radios = dict()
  43.  
  44.     # -----------------LABELS------------------------------------------------
  45.     def get_label(self, place_on, x, y, text_l, w, p, color='blue', font=11, bg_color=None):
  46.         label = tk.Label(place_on, text=text_l, font=('Times New Roman', font, 'italic'),
  47.                          padx=5, pady=1, bd=-1, width=w, anchor=p, fg=color, bg=bg_color)
  48.         label.place(x=x, y=y)
  49.         return label
  50.  
  51.     # -----------------LABELS with underline------------------------------------------------
  52.  
  53.     def get_label_un(self, geo, text_l, x, y, p=None, w=15, color='black', bg_color=None, bg_border=None, j=None, name=None):
  54.         border_colour = tk.Frame(geo, background=bg_border)
  55.         border_colour.pack(padx=40, pady=40)
  56.         border_colour.place(x=x, y=y)
  57.         label = tk.Label(border_colour, text=text_l, font=('Times New Roman', 10, 'italic'), bg=bg_color,
  58.                          padx=5, pady=5, bd=-1, width=w, anchor=p, fg=color, justify=j, name=name)
  59.         label.pack(padx=0, pady=(0, 1))
  60.         return label
  61.  
  62.     # -----------------BUTTONS------------------------------------------------
  63.     def get_button(self, place_on, x: int, y: int, text: str, fg: str):
  64.         btn = tk.Button(place_on, text=text, font=('Bookman Old Style Bold', 8), fg=fg, height=1, width=10)
  65.         btn.place(x=x, y=y)
  66.         return btn
  67.  
  68.     def get_save_button(self, place_on, x: int, y: int, text='МАРКИРАЙ'):
  69.         return self.get_button(place_on, x, y, text=text, fg="blue")
  70.  
  71.     def get_ok_button(self, place_on, x: int, y: int):
  72.         return self.get_button(place_on, x, y, "ЗАПИС", fg="green")
  73.  
  74.     def get_help_button(self, place_on, x: int, y: int):
  75.         return self.get_button(place_on, x, y, "Помощ", fg="black")
  76.  
  77.     def get_correction_button(self, place_on, x: int, y: int):
  78.         return self.get_button(place_on, x, y, "КОРЕКЦИИ", fg="darkred")
  79.  
  80.     def get_correction_report(self, place_on, x: int, y: int):
  81.         return self.get_button(place_on, x, y, "СПРАВКА", fg="green")
  82.  
  83.     def get_correction_esc(self, place_on, x: int, y: int):
  84.         return self.get_button(place_on, x, y, "ОТКАЗ", fg="red")
  85.  
  86.     def get_connection(self, place_on, x: int, y: int):
  87.         return self.get_button(place_on, x, y, "СВЪРЗВАНЕ", fg="blue")
  88.  
  89.     def get_start(self, place_on, x: int, y: int):
  90.         return self.get_button(place_on, x, y, "СТАРТ", fg="green")
  91.  
  92.     # -----------------LABEL FRAMES------------------------------------------------
  93.     def get_label_frame(self, place_on, text):
  94.         lab_frame = tk.LabelFrame(place_on, text=text, relief=tk.GROOVE, bd=2, width=10,
  95.                                   font=('Timesbd', 9, 'italic'), fg='blue')
  96.         return lab_frame
  97.  
  98.     # -----------------RADIO BUTTONS------------------------------------------------
  99.     def get_radio_butt(self, place_on, size, orient, list_buttons: dict, var):
  100.         y = 0
  101.         for radio in list_buttons:
  102.             self.radios[radio] = tk.Radiobutton(place_on, text=list_buttons[radio], width=size, anchor=orient,
  103.                                                 variable=var, value=radio, state='normal', activeforeground='red',
  104.                                                 tristatevalue=' ')
  105.             self.radios[radio].grid(row=y, column=0, padx=10, sticky='w')
  106.             y += 1
  107.  
  108.     # -----------------FRAMES------------------------------------------------
  109.     def get_frame(self, place_on):
  110.         frame = ttk.Frame(place_on)
  111.         return frame
  112.  
  113.     # -----------------TREEVIEW------------------------------------------------
  114.     def get_treeview(self, place_on, **columns):
  115.         cols = []
  116.         for col in columns:
  117.             cols.append(col)
  118.         self.style = ttk.Style(place_on)  # --TODO с place_on стилът се оправи !!!!
  119.         self.style.theme_use('default')  # За Антетката !
  120.         c = self.style.configure('Treeview', background='lightgrey', foreground='black', fieldbackground='red',
  121.                                  rowheight=25)
  122.         treeview_acc = ttk.Treeview(place_on, show='headings', columns=cols, height=0)  # style=c
  123.         y = 1
  124.         w = int()
  125.         anc_h = ''
  126.         anc_c = ''
  127.         for head in columns:
  128.             # print('head: ', head)
  129.             cols.append(head)
  130.             if head == 'type': w = 100; anc_h = 'w'; anc_c = 'w'
  131.             elif head == 'code': w = 70; anc_h = 'w'; anc_c = 'w'
  132.             elif head == 'code_i': w = 60; anc_h = 'w'; anc_c = 'w'
  133.             elif head == 'acc_name': w = 300; anc_h = 'w'; anc_c = 'w'
  134.             elif head == 'open_d': w = 100; anc_h = 'center'; anc_c = 'e'
  135.             elif head == 'open_di': w = 80; anc_h = 'center'; anc_c = 'e'
  136.             elif head == 'open_c': w = 100; anc_h = 'center'; anc_c = 'e'
  137.             elif head == 'open_ci': w = 80; anc_h = 'center'; anc_c = 'e'
  138.             elif head == 'turn_d': w = 100; anc_h = 'center'; anc_c = 'e'
  139.             elif head == 'turn_di': w = 80; anc_h = 'center'; anc_c = 'e'
  140.             elif head == 'turn_c': w = 100; anc_h = 'center'; anc_c = 'e'
  141.             elif head == 'turn_ci': w = 80; anc_h = 'center'; anc_c = 'e'
  142.             elif head == 'close_d': w = 100; anc_h = 'center'; anc_c = 'e'
  143.             elif head == 'close_di': w = 80; anc_h = 'center'; anc_c = 'e'
  144.             elif head == 'close_c': w = 100; anc_h = 'center'; anc_c = 'e'
  145.             elif head == 'close_ci': w = 80; anc_h = 'center'; anc_c = 'e'
  146.             treeview_acc.heading(head, text=columns[head], anchor=anc_h)
  147.             treeview_acc.column(head, width=w, minwidth=60, anchor=anc_c)
  148.             y += 1
  149.         self.style.map('Treeview', background=[('selected', 'blue')])
  150.         treeview_acc.pack(side=tk.LEFT)
  151.         return treeview_acc
  152.  
  153.     def get_acc_report_treeview(self, place_on, **columns):
  154.         cols = []
  155.         for col in columns:
  156.             cols.append(col)
  157.         self.style = ttk.Style(place_on)  # --TODO с place_on стилът се оправи !!!!
  158.         self.style.theme_use('default')  # За Антетката !
  159.         c = self.style.configure('Treeview', background='lightgrey', foreground='black', fieldbackground='red',
  160.                                  rowheight=25)
  161.         treeview_acc = ttk.Treeview(place_on, show='headings', columns=cols, height=0)  # style=c
  162.         y = 1
  163.         w = int()
  164.         anc_h = ''
  165.         anc_c = ''
  166.  
  167.         #{"c_acc": 'Кредит по с/ка', "od": 'Оборот Дт', 'oc': 'Оборот Кт', 'd_acc': 'Дебит по с/ка'}
  168.         for head in columns:
  169.             # print('head: ', head)
  170.             cols.append(head)
  171.             if head == 'c_acc': w = 100; anc_h = 'w'; anc_c = 'w'
  172.             elif head == 'od': w = 70; anc_h = 'w'; anc_c = 'w'
  173.             elif head == 'oc': w = 60; anc_h = 'w'; anc_c = 'w'
  174.             elif head == 'd_acc': w = 300; anc_h = 'w'; anc_c = 'w'
  175.             treeview_acc.heading(head, text=columns[head], anchor=anc_h)
  176.             treeview_acc.column(head, width=w, minwidth=60, anchor=anc_c)
  177.             y += 1
  178.         self.style.map('Treeview', background=[('selected', 'blue')])
  179.         treeview_acc.pack(side=tk.LEFT)
  180.         return treeview_acc
  181.  
  182.     # -----------------SPINBOX------------------------------------------------
  183.     def get_spinbox(self, place_on, min_val, max_val, w, just, current_val, x, y):
  184.         spin_box = tk.Spinbox(place_on, from_=min_val, to=max_val, width=w, justify=just, textvariable=current_val,
  185.                               wrap=True)
  186.         spin_box.place(x=x, y=y)
  187.         return spin_box
  188.  
  189.     def get_list_spinbox(self, place_on, list_values, w, just, current_val, x, y):
  190.         spin_box = tk.Spinbox(place_on, values=list_values, width=w, justify=just, textvariable=current_val, wrap=True)
  191.         spin_box.place(x=x, y=y)
  192.         return spin_box
  193.  
  194.     def get_scrollbar(self, place_on, orient, side, fill):
  195.         scr_bar = ttk.Scrollbar(place_on, orient=orient)
  196.         scr_bar.pack(side=side, fill=fill)
  197.         return scr_bar
  198.  
  199.     def get_pop_message(self, parent_window, function_on_answer, text, number_buttons, text_color):
  200.         dialog = PopMessage(parent_window, function_on_answer, text, number_buttons, text_color)
  201.         return dialog
  202.  
  203.     def get_entry(self, geo, x, y, w, j=tk.LEFT, name=None):
  204.         entry = tk.Entry(geo, bd=4, width=w, name=name, justify=j)
  205.         entry.place(x=x, y=y)
  206.         return entry
  207.  
  208.  
  209. if __name__ == '__main__':
  210.     window = tk.Tk()
  211.     window.title('Оборотна ведомост')
  212.     window.geometry('1200x840+200+10')
  213.     window.resizable(True, False)
  214.     window.attributes('-topmost', 'true')
  215.  
  216.     window.mainloop()
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement