Advertisement
Mika_aaa

main_window.py

Mar 12th, 2023 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.57 KB | None | 0 0
  1. import time
  2. import multiprocessing
  3. from re import match
  4. from tkinter import Button, Label, Frame, Entry, Tk, PhotoImage
  5. from ping_window import NewProcessPingWindow
  6.  
  7. logo_raw = 'very_long_byte_string with converted to bytes .ico'
  8. btn_icon_raw = 'very_long_byte_string with converted to bytes .png'
  9.  
  10. def check_packages(symbol):
  11.     return match("^\d{,7}$", symbol) is not None or symbol is None
  12.  
  13.  
  14. def check_ttl(symbol):
  15.     if match("^\d*$", symbol) is not None and symbol != '':
  16.         if int(symbol) > 255 or int(symbol) < 1:
  17.             return symbol is None
  18.     return match("^\d*$", symbol) is not None or symbol is None
  19.  
  20.  
  21. def check_size(symbol):
  22.     if match("^\d{,4}$", symbol) is not None and symbol != '':
  23.         if int(symbol) > 1472:
  24.             return symbol is None
  25.     return match("^\d{,4}$", symbol) is not None or symbol is None
  26.  
  27.  
  28. def check_float(symbol):
  29.     if match("^\d*\.{,1}\d{,3}$", symbol) is not None and symbol != '':
  30.         if symbol != '.' and float(symbol) > 3600.999:
  31.             return symbol is None
  32.     return match("^\d*\.{,1}\d{,3}$", symbol) is not None or symbol is None
  33.  
  34.  
  35. def main_window():
  36.     time_passed = time.time()
  37.  
  38.     def ping():
  39.         NewProcessPingWindow(set_daemon=True,
  40.                              url=address_entry.get(),
  41.                              packages=packages_entry.get(),
  42.                              interval=interval_entry.get(),
  43.                              timeout=timeout_entry.get(),
  44.                              ttl=ttl_entry.get(),
  45.                              size=size_entry.get(),
  46.                              window_size={'width': screen_width, 'height': screen_height},
  47.                              logo_raw=logo_raw).start()
  48.         # print(len(multiprocessing.active_children()))
  49.         # print(multiprocessing.active_children())
  50.  
  51.     def _onKeyRelease(event):
  52.         ctrl = (event.state & 0x4) != 0
  53.         if event.keycode == 88 and ctrl and event.keysym.lower() != "x":
  54.             event.widget.event_generate("<<Cut>>")
  55.  
  56.         if event.keycode == 86 and ctrl and event.keysym.lower() != "v":
  57.             event.widget.event_generate("<<Paste>>")
  58.  
  59.         if event.keycode == 67 and ctrl and event.keysym.lower() != "c":
  60.             event.widget.event_generate("<<Copy>>")
  61.  
  62.     root = Tk()
  63.  
  64.     root.title("Ping-Pong")
  65.     logo_icon = PhotoImage(data=logo_raw)
  66.     root.iconphoto(False, logo_icon)
  67.     root.pack_propagate(True)
  68.     root.configure(background='#2b2b2b')
  69.     root.resizable(width=False, height=False)
  70.     root.bind_all("<Key>", _onKeyRelease, "+")
  71.  
  72.     container0 = Frame(root, background='#2b2b2b', padx=40, pady=40)
  73.     container0.pack(anchor='center', expand=True)
  74.     container1 = Frame(container0, background='#3c3f41', pady=20, padx=20)
  75.     container1.pack(side='left', expand=True)
  76.     container2 = Frame(container0, background='#2b2b2b', pady=8, padx=8)
  77.     container2.pack(anchor='w', side='right', expand=True)
  78.  
  79.     btn_icon = PhotoImage(data=btn_icon_raw)
  80.     btn_ping = Button(container2, image=btn_icon, relief='flat', bd=0, background='#2b2b2b', activebackground='#2b2b2b')
  81.     btn_ping.config(command=lambda: ping())
  82.     btn_ping.pack(side='right')
  83.  
  84.     buff_column = Label(container1, text='', padx=0, border=0, background='#2b2b2b')
  85.     buff_column.grid(row=0, column=1, rowspan=7, sticky='nsew')
  86.  
  87.     address_entry = Entry(container1,
  88.                           font='Arial 12',
  89.                           width=20,
  90.                           relief='flat', bd=5,
  91.                           bg='#2b2b2b',
  92.                           fg='#D6ECFF',
  93.                           insertbackground='#D6ECFF')
  94.     address_entry.insert(0, 'google.com')
  95.     address_entry.bind('<Return>', lambda x: ping())
  96.     address_entry.bind('<FocusIn>', lambda x: address_entry.select_range(0, 'end'))
  97.  
  98.     address_label = Label(container1, text='Address', bg='#3c3f41', fg='#D6ECFF', bd=0, font='Arial 12')
  99.     address_label.grid(row=0, column=0, sticky='e', padx=(0, 10), pady=(0, 10))
  100.     address_entry.grid(row=0, column=2, sticky='w', padx=(10, 0), pady=(0, 10))
  101.  
  102.     packages_entry = Entry(container1,
  103.                            font='Arial 12',
  104.                            width=20,
  105.                            relief='flat', bd=5,
  106.                            bg='#2b2b2b',
  107.                            fg='#90CDFF',
  108.                            insertbackground='#90CDFF',
  109.                            validate="key",
  110.                            validatecommand=(root.register(check_packages), "%P"))
  111.     packages_entry.bind('<Return>', lambda x: ping())
  112.     packages_entry.bind('<FocusIn>', lambda x: packages_entry.select_range(0, 'end'))
  113.     packages_entry.insert(0, '4')
  114.     packages_label = Label(container1, text='Packages', bg='#3c3f41', fg='#90CDFF', bd=0, font='Arial 12')
  115.     packages_label.grid(row=2, column=0, sticky='e', padx=(0, 10), pady=(0, 10))
  116.     packages_entry.grid(row=2, column=2, sticky='w', padx=(10, 0), pady=(0, 10))
  117.  
  118.     interval_entry = Entry(container1,
  119.                            font='Arial 12',
  120.                            width=20,
  121.                            relief='flat', bd=5,
  122.                            bg='#2b2b2b',
  123.                            fg='#90CDFF',
  124.                            insertbackground='#90CDFF',
  125.                            validate="key",
  126.                            validatecommand=(root.register(check_float), "%P"))
  127.     interval_entry.bind('<Return>', lambda x: ping())
  128.     interval_entry.bind('<FocusIn>', lambda x: interval_entry.select_range(0, 'end'))
  129.     interval_entry.insert(0, '1')
  130.     interval_label = Label(container1, text='Frequency', bg='#3c3f41', fg='#90CDFF', bd=0, font='Arial 12')
  131.     interval_label.grid(row=3, column=0, sticky='e', padx=(0, 10), pady=(0, 10))
  132.     interval_entry.grid(row=3, column=2, sticky='w', padx=(10, 0), pady=(0, 10))
  133.  
  134.     timeout_entry = Entry(container1,
  135.                           font='Arial 12',
  136.                           width=20,
  137.                           relief='flat', bd=5,
  138.                           bg='#2b2b2b',
  139.                           fg='#90CDFF',
  140.                           insertbackground='#90CDFF',
  141.                           validate="key",
  142.                           validatecommand=(root.register(check_float), "%P"))
  143.     timeout_entry.bind('<Return>', lambda x: ping())
  144.     timeout_entry.bind('<FocusIn>', lambda x: timeout_entry.select_range(0, 'end'))
  145.     timeout_entry.insert(0, '4')
  146.     timeout_label = Label(container1, text='Timeout', bg='#3c3f41', fg='#90CDFF', bd=0, font='Arial 12')
  147.     timeout_label.grid(row=4, column=0, sticky='e', padx=(0, 10), pady=(0, 10))
  148.     timeout_entry.grid(row=4, column=2, sticky='w', padx=(10, 0), pady=(0, 10))
  149.  
  150.     size_entry = Entry(container1,
  151.                        font='Arial 12',
  152.                        width=20,
  153.                        relief='flat', bd=5,
  154.                        bg='#2b2b2b',
  155.                        fg='#90CDFF',
  156.                        insertbackground='#90CDFF',
  157.                        validate="key",
  158.                        validatecommand=(root.register(check_size), "%P"))
  159.     size_entry.bind('<Return>', lambda x: ping())
  160.     size_entry.bind('<FocusIn>', lambda x: size_entry.select_range(0, 'end'))
  161.     size_entry.insert(0, '32')
  162.     size_label = Label(container1, text='Size', bg='#3c3f41', fg='#90CDFF', bd=0, font='Arial 12')
  163.     size_label.grid(row=5, column=0, sticky='e', padx=(0, 10), pady=(0, 10))
  164.     size_entry.grid(row=5, column=2, sticky='w', padx=(10, 0), pady=(0, 10))
  165.  
  166.     ttl_entry = Entry(container1,
  167.                       font='Arial 12',
  168.                       width=20,
  169.                       relief='flat', bd=5,
  170.                       bg='#2b2b2b',
  171.                       fg='#90CDFF',
  172.                       insertbackground='#90CDFF',
  173.                       validate="key",
  174.                       validatecommand=(root.register(check_ttl), "%P"))
  175.     ttl_entry.bind('<Return>', lambda x: ping())
  176.     ttl_entry.bind('<FocusIn>', lambda x: ttl_entry.select_range(0, 'end'))
  177.     ttl_entry.insert(0, '128')
  178.     ttl_label = Label(container1, text='TTL', bg='#3c3f41', fg='#90CDFF', bd=0, font='Arial 12')
  179.     ttl_label.grid(row=6, column=0, sticky='e', padx=(0, 10), pady=(0, 0))
  180.     ttl_entry.grid(row=6, column=2, sticky='w', padx=(10, 0), pady=(0, 0))
  181.  
  182.  
  183.  
  184.     root.update()
  185.     screen_width = root.winfo_width()
  186.     screen_height = root.winfo_height()
  187.     root.minsize(width=screen_width, height=screen_height)
  188.  
  189.     time_passed = time.time() - time_passed
  190.     print(time_passed)
  191.     root.mainloop()
  192.  
  193.  
  194. if __name__ == '__main__':
  195.     multiprocessing.freeze_support()
  196.     main_window()
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement