Advertisement
ALEXANDAR_GEORGIEV

autocomplete_my

Jun 17th, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.86 KB | None | 0 0
  1. import tkinter as tk
  2. import re  # import regular expression library
  3. from tkinter import END
  4.  
  5. my_w = tk.Tk()
  6. my_w.geometry("410x400")  # Size of the window
  7. my_w.title("plus2net.com")  # Adding a title
  8. font1 = ('Times', 24, 'bold')  # font size and style
  9. l0 = tk.Label(text='Autocomplete', font=font1)  # adding label at top
  10. l0.grid(row=0, column=1)
  11. # data source list,
  12. my_list = ['aecde', 'adba', 'acbd', 'abcd', 'abded',
  13.            'bdbd', 'baba', 'bcbc', 'bdbd']
  14.  
  15.  
  16. def my_upd(my_widget):  # On selection of option
  17.     my_w = my_widget.widget
  18.     index = int(my_w.curselection()[0])  # position of selection
  19.     value = my_w.get(index)  # selected value
  20.     e1_str.set(value)  # set value for string variable of Entry
  21.     l1.delete(0, END)  # Delete all elements of Listbox
  22.  
  23.  
  24. def my_down(my_widget):  # down arrow is clicked
  25.     l1.focus()  # move focus to Listbox
  26.     l1.selection_set(0)  # select the first option
  27.  
  28.  
  29. e1_str = tk.StringVar()  # string variable
  30. e1 = tk.Entry(my_w, textvariable=e1_str, font=font1)  # entry
  31. e1.grid(row=1, column=1, padx=10, pady=0)
  32. # listbox
  33. l1 = tk.Listbox(my_w, height=6, font=font1, relief='flat',
  34.                 bg='SystemButtonFace', highlightcolor='SystemButtonFace')
  35. l1.grid(row=2, column=1)
  36.  
  37.  
  38. def get_data(*args):  # populate the Listbox with matching options
  39.     search_str = e1.get()  # user entered string
  40.     l1.delete(0, END)  # Delete all elements of Listbox
  41.     for element in my_list:
  42.         if re.match(search_str, element, re.IGNORECASE):
  43.             l1.insert(tk.END, element)  # add matching options to Listbox
  44.  
  45.  
  46. # l1.bind('<<ListboxSelect>>', my_upd)
  47. e1.bind('<Down>', my_down)  # down arrow key is pressed
  48. l1.bind('<Right>', my_upd)  # right arrow key is pressed
  49. l1.bind('<Return>', my_upd)  # return key is pressed
  50. l1.bind("<ButtonPress-1>", my_upd)  # return left-mouse clicked
  51. e1_str.trace('w', get_data)  #
  52. # print(my_w['bg']) # reading background colour of window
  53. my_w.mainloop()  # Keep the window open
  54.  
  55. ###############################################################################
  56. from tkinter import *
  57. tray_window = None
  58.  
  59.  
  60. def loss_focus(event):
  61.     print("loss_focus event")
  62.     if tray_window is not None:
  63.         if event.widget is not tray_window:
  64.             tray_window.destroy()
  65.  
  66.  
  67. def show_tray_window(event):
  68.     global tray_window
  69.     #print("key up")
  70.     if tray_window is None:
  71.         tray_window = Toplevel(entry)
  72.         x = entry.winfo_rootx()     # Това е вградена функция
  73.         y = entry.winfo_rooty() + entry.winfo_height() + 1
  74.         tray_window.wm_overrideredirect(1)
  75.  
  76.         tray_window.wm_geometry("+%d+%d" % (x, y))
  77.         label = Label(tray_window, text="Neshto", justify=LEFT,
  78.                       background="#ffffe0", relief=SOLID, borderwidth=1)
  79.         label.pack()
  80.         tray_window.bind('<FocusOut>', loss_focus)
  81.  
  82.  
  83. root = Tk()
  84. root.geometry("300x300")
  85. root.bind('<FocusOut>', loss_focus)
  86.  
  87. entry = Entry(root)
  88. entry.bind('<Key>', show_tray_window)
  89.  
  90. entry.pack()
  91.  
  92. #tray_window = Toplevel(entry)
  93.  
  94. root.mainloop()
  95.  
  96. ################################################## https://www.youtube.com/watch?v=Bn1n1diGv_0
  97. from tkinter import *
  98. import pandas as pd
  99. from tkinter import ttk, filedialog
  100.  
  101. root = Tk()
  102. root.title('Codemy.com - Excel To Treeview')
  103. root.iconbitmap('c:/gui/codemy.ico')
  104. root.geometry('700x500')
  105. # Create frame
  106. my_frame = Frame(root)
  107. my_frame.pack(pady=20)
  108. # Create treeview
  109. my_tree = ttk.Treeview(my_frame)
  110.  
  111.  
  112. # File open function
  113. def file_open():
  114.     filename = filedialog.askopenfilename(
  115.         initialdir="C:/gui/",
  116.         title="Open A File",
  117.         filetypes=(("xlsx files", "*.xlsx"), ("All Files", "*.*"))
  118.     )
  119.     if filename:
  120.         try:
  121.             filename = r"{}".format(filename)
  122.             df = pd.read_excel(filename)
  123.  
  124.         except ValueError:
  125.             my_label.config(text="File Couldn't Be Opened ...try again !")
  126.         except FileNotFoundError:
  127.             my_label.config(text="File Couldn't Be Opened ...try again !")
  128.     # Clear old treeview
  129.     clear_tree()
  130.  
  131.     # St up new treeview
  132.     my_tree["column"] = list(df.colimns)
  133.     my_tree["show"] = "headings"
  134.     # Loop thru column list for headers
  135.     for column in my_tree["column"]:
  136.         my_tree.heading(column, text=column)
  137.  
  138.     # Put data in treview
  139.     df_rows = df.to_numpy().tolist()
  140.     for row in df_rows:
  141.         my_tree.insert("", "end", values=row)
  142.     # Pack the treeview finally
  143.     my_tree.pack()
  144.  
  145.  
  146.  
  147. def clear_tree():
  148.     my_tree.delete(*my_tree.get_children())
  149.  
  150.  
  151. # Add a menu
  152. my_menu = Menu(root)
  153. root.config(menu=my_menu)
  154.  
  155. # Add menu dropdown
  156. file_menu = Menu(my_menu, tearoff=False)
  157. my_menu.add_cascade(label="Spreadsheets", menu=file_menu)
  158. file_menu.add_command(label="Open", command=file_open)
  159.  
  160. my_label = Label(root, text='')
  161. my_label.pack
  162.  
  163. root.mainloop(pady=20)
  164.  
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement