Advertisement
here2share

# tk_autosuggestion.py

Dec 15th, 2022 (edited)
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.91 KB | None | 0 0
  1. # tk_autosuggestion.py
  2.  
  3. import tkinter as tk
  4. root = tk.Tk()
  5. user_input = tk.StringVar()
  6. suggestions = []
  7. entry = tk.Entry(root, textvariable=user_input)
  8. entry.bind("<Key>", update_suggestions)
  9.  
  10. autosuggestion = ['root = Tk()', 'root.geometry("widthxheight+xoffset+yoffset")', 'root.title("string")', 'var$ = ', '(,[dict~])',
  11.                 'Label(root, text="string")', 'Button(root, text="string", command=function)', 'Entry(root, textvariable=var$)',
  12.                 'Checkbutton(root, text="string", variable=var$)', 'Radiobutton(root, text="string", variable=var$)',
  13.                 'Listbox(root, listvariable=var$)', 'Text(root, width=number, height=number)',
  14.                 'Frame(root)', 'Scrollbar(frame)', 'Menu(root)', 'Menu(menu)',
  15.                 'canvas = Canvas(root, width=number, height=number)', 'image = PhotoImage(file="filename.gif")',
  16.                 'scale = Scale(root, from_=number, to=number, orient=HORIZONTAL)', 'spinbox = Spinbox(root, from_=number, to=number)',
  17.                 'message = Message(root, text="string")', 'optionmenu = OptionMenu(root, var$, "choice1", "choice2", "choice3")',
  18.                 'frame.pack()', 'scrollbar.pack(side=RIGHT, fill=Y)', 'menu.add_cascade(label="string", menu=submenu)',
  19.                 'canvas.pack()', 'canvas.create_image(x, y, image=image)', 'scale.pack()', 'spinbox.pack()', 'message.pack()',
  20.                 'optionmenu.pack()', 'widget.grid(row=number, column=number, ...)', 'widget.place(x=number, y=number, ...)',["print", ["(", ["=", ["sep", "end", "file"]], ")"],
  21. ["import", ["module~"]],
  22. ["from", ["module~", "import", ["func~"]],
  23. ["class", ["classname", ["(", ["parentclass~"], ")"], ":"],
  24. ["def", ["funcname", ["(", ["arg~"], ")"], ":"],
  25. ["return", ["expr"]],
  26. ["if", ["expr"], ":",
  27. ["elif", ["expr"], ":",
  28. ["else:"],
  29. ["for", ["var", "in", "iter"], ":"],
  30. ["while", ["expr"], ":"],
  31. ["try:",
  32. ["except", ["Exception", "as", "var"], ":"],
  33. ["with", ["expr", "as", "var"], ":"],
  34. ["raise", ["Exception"]],
  35. ["break"],
  36. ["continue"],
  37. ["pass"],
  38. ["del", ["var"]],
  39. ["assert", ["expr"]],
  40. ["yield", ["expr"]],
  41. ["lambda", ["arg~"], ":", ["expr"]],
  42. ["global", ["var~"]],
  43. ["nonlocal", ["var~"]],
  44. ["async", ["def", ["funcname", ["(", ["arg~"], ")"], ":"],
  45. ["await", ["expr"]],
  46. ["range", ["start", "stop", "step"]],
  47. ["in"],
  48. ["not in"],
  49. ["and"],
  50. ["or"],
  51. ["not"],
  52. ["len", ["obj"]],
  53. ["type", ["obj"]],
  54. ["str", ["obj"]],
  55. ["repr", ["obj"]],
  56. ["int", ["x", "base"]],
  57. ["float", ["x"]],
  58. ["complex", ["real", "imag"]],
  59. ["divmod", ["x", "y"]],
  60. ["pow", ["x", "y", "z"]],
  61. ["round", ["number", "ndigits"]],
  62. ["max", ["iter", "*args", "key"]],
  63. ["min", ["iter", "*args", "key"]],
  64. ["sum", ["iter", "start"]],
  65. ["any", ["iter"]],
  66. ["all", ["iter"]],
  67. ["sorted", ["iter", "key", "reverse"]],
  68. ["reversed", ["seq"]],
  69. ["enumerate", ["iter", "start"]],
  70. ["zip", ["*iters"]],
  71. ["map", ["function", "iter", "*iters"]],
  72. ["filter", ["function", "iter"]],
  73. ["abs", ["x"]],
  74. ["chr", ["i"]],
  75. ["ord", ["c"]],
  76. ["bin", ["x"]],
  77. ["oct", ["x"]],
  78. ["hex", ["x"]],
  79. ["bool", ["x"]],
  80. ["bytearray", ["source", "encoding", "errors"]],
  81. ["bytes", ["source", "encoding", "errors"]],
  82. ["complex", ["real", "imag"]],
  83. ["dict", ["mapping", "**kwargs"]],
  84. ["dir", ["obj"]],
  85. ["frozenset", ["iter"]],
  86. ["list", ["iter"]],
  87. ["set", ["iter"]],
  88. ["tuple", ["iter"]],
  89. ["type", ["obj", "base_type", "dict"]],
  90. ["vars", ["obj"]],
  91. ["repr", ["obj"]],
  92. ["format", ["value", "format_spec"]],
  93. ["hash", ["obj"]],
  94. ["memoryview", ["obj"]],
  95. ["property", ["fget", "fset", "fdel", "doc"]],
  96. ["staticmethod", ["function"]],
  97. ["classmethod", ["function"]],
  98. ["isinstance", ["obj", "classinfo"]],
  99. ["compile", ["source", "filename", "mode", "flags", "dont_inherit"]],
  100. ["exec", ["source", "globals", "locals"]],
  101. ["eval", ["expression", "globals", "locals"]],
  102. ["globals", [""]],
  103. ["locals", [""]],
  104. ["vars", [""]],
  105. ["dir", [""]],
  106. ["help", ["obj"]],
  107. ["ascii", ["obj"]],
  108. ["repr", ["obj"]],
  109. ["input", ["prompt"]],
  110. ["print", ["*objs", "sep", "end", "file", "flush"]],
  111. ["delattr", ["obj", "name"]],
  112. ["getattr", ["obj", "name", "default"]],
  113. ["setattr", ["obj", "name", "value"]],
  114. ["hasattr", ["obj", "name"]],
  115. ["id", ["obj"]],
  116. ["isinstance", ["obj", "classinfo"]],
  117. ["iter", ["obj", "sentinel"]],
  118. ["next", ["iterator", "default"]],
  119. ["len", ["obj"]],
  120. ["reversed", ["sequence"]],
  121. ["sorted", ["iterable", "key", "reverse"]],
  122. ["slice", ["start", "stop", "step"]],
  123. ["sum", ["iterable", "start"]],
  124. ["type", ["obj"]],
  125. ["zip", ["*iterables"]]]]
  126.  
  127. listbox = tk.Listbox(root)
  128. def update_suggestions(event):
  129.     user_input = event.widget.get()
  130.     suggestions = []
  131.    
  132.     # Update the suggestions list based on the user's input
  133.     for command in autosuggestion:
  134.         if command.startswith(user_input):
  135.             suggestions.append(command)
  136.    
  137.     # Clear the listbox and refill it with the updated suggestions
  138.     listbox.delete(0, tk.END)
  139.     for suggestion in suggestions:
  140.         listbox.insert(tk.END, suggestion)
  141.  
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement