Advertisement
here2share

# tk_nocode_2024.py

Jan 4th, 2024
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.70 KB | None | 0 0
  1. # tk_nocode_2024.py ZZZ
  2.  
  3. import tkinter as tk
  4. import tkinter.colorchooser as colorchooser
  5.  
  6. def create_widget(widget_type, x, y):
  7.     if widget_type == "button":
  8.         return tk.Button(root, text="Button", width=10, height=2)
  9.     elif widget_type == "label":
  10.         return tk.Label(root, text="Label", bg="lightgrey", relief="raised")
  11.     elif widget_type == "entry":
  12.         return tk.Entry(root, width=15)
  13.     else:
  14.         raise ValueError("Invalid widget type")
  15.  
  16. def add_widget(widget_type, x, y):
  17.     widget = create_widget(widget_type, x, y)
  18.     widget.place(x=x, y=y)
  19.     widgets.append(widget)
  20.  
  21. def delete_widget(event):
  22.     widget = event.widget
  23.     widget.destroy()
  24.     widgets.remove(widget)
  25.  
  26. def select_widget(event):
  27.     global selected_widget
  28.     selected_widget = event.widget
  29.     for widget in widgets:
  30.         widget.bind("<Button-1>", lambda e: select_widget(e))
  31.  
  32. def deselect_widgets():
  33.     for widget in widgets:
  34.         widget.bind("<Button-1>", lambda e: select_widget(e))
  35.  
  36. def change_text():
  37.     if selected_widget:
  38.         text = text_entry.get()
  39.         selected_widget.configure(text=text)
  40.  
  41. def bring_to_front():
  42.     if selected_widget:
  43.         selected_widget.lift()
  44.  
  45. def send_to_back():
  46.     if selected_widget:
  47.         selected_widget.lower()
  48.  
  49. def create_countdown_widget():
  50.     countdown_label = tk.Label(root, text="Countdown Timer:", font=("Arial", 12))
  51.     countdown_label.place(x=50, y=100)
  52.  
  53. def show_properties():
  54.     if selected_widget:
  55.         properties_window = tk.Toplevel(root)
  56.         properties_window.title("Widget Properties")
  57.         properties_label = tk.Label(properties_window, text=f"Selected Widget: {type(selected_widget).__name__}")
  58.         properties_label.pack()
  59.  
  60. def create_hyperlink():
  61.     hyperlink_label = tk.Label(root, text="Click here!", fg="blue", cursor="hand2")
  62.     hyperlink_label.pack()
  63.     hyperlink_label.bind("<Button-1>", lambda event: print("Hyperlink clicked!"))
  64.  
  65. def upload_image():
  66.     upload_button = tk.Button(root, text="Upload Image")
  67.     upload_button.pack()
  68.  
  69. def toggle_snap_to_grid():
  70.     # Assuming grid snapping is needed for widget placement
  71.     canvas.bind("<Button-1>", lambda event: snap_to_grid(event))
  72.  
  73. def snap_to_grid(event):
  74.     x = round(event.x / 10) * 10
  75.     y = round(event.y / 10) * 10
  76.     create_widget("button", x, y)  # Create a button at snapped coordinates
  77.  
  78. def apply_font_properties():
  79.     if selected_widget:
  80.         font_style = "Arial"
  81.         font_size = 12
  82.         font_color = "black"
  83.         selected_widget.config(font=(font_style, font_size), fg=font_color)
  84.  
  85. def pick_color():
  86.     color = tk.colorchooser.askcolor()
  87.     return color
  88.  
  89. def apply_group_expand_fill(action):
  90.     if selected_widget:
  91.         if action == "group":
  92.             print("Grouping widgets...")
  93.         elif action == "expand":
  94.             print("Expanding widgets...")
  95.         elif action == "both":
  96.             print("Filling both directions...")
  97.         elif action == "x":
  98.             print("Filling in X direction...")
  99.         elif action == "y":
  100.             print("Filling in Y direction...")
  101.            
  102. def apply_text_alignment(alignment):
  103.     text_area.tag_configure("align", justify=alignment)
  104.     text_area.tag_add("align", "1.0", "end")
  105.            
  106. def toggle_fullscreen():
  107.     root.attributes("-fullscreen", not root.attributes("-fullscreen"))
  108.    
  109. def show_widgets_control():
  110.     global widget_window
  111.     try:
  112.         widget_window.destroy()
  113.     except:
  114.         pass
  115.  
  116.     def create_button(text, command=None):
  117.         button = tk.Button(widget_window, text=text, command=command, width=25)
  118.         button.config(width=50)
  119.         button.pack()
  120.         widgets_in_window.append(button)
  121.  
  122.     widget_window = tk.Toplevel(root)
  123.     widget_window.title("Widget Window")
  124.     widgets_in_window = []
  125.  
  126.     # Change Text section
  127.     text_entry_label = tk.Label(widget_window, text="Change Text:")
  128.     text_entry_label.pack()
  129.     text_entry = tk.Entry(widget_window)
  130.     text_entry.pack(fill='x')
  131.  
  132.     create_button("Apply Text Change", change_text)
  133.     create_button("Bring to Front", bring_to_front)
  134.     create_button("Send to Back", send_to_back)
  135.     create_button("Add Countdown Timer", create_countdown_widget)
  136.     create_button("Show Widget Properties", show_properties)
  137.     create_button("Toggle Fullscreen", toggle_fullscreen)
  138.     create_button("Create Hyperlink", create_hyperlink)
  139.     create_button("Upload Image", upload_image)
  140.     create_button("Toggle Snap to Grid", toggle_snap_to_grid)
  141.  
  142.     for alignment in ["Align Left", "Align Center", "Align Right"]:
  143.         create_button(alignment, lambda a=alignment: apply_text_alignment(a.lower()))
  144.  
  145.     create_button("Apply Font Properties", apply_font_properties)
  146.     create_button("Pick Color", pick_color)
  147.  
  148.     for action in ["Group", "Expand", "Fill (Both)", "Fill (X)", "Fill (Y)"]:
  149.         create_button(action, lambda a=action.lower(): apply_group_expand_fill(a.lower()))
  150.  
  151.     widget_window.lift()
  152.  
  153.  
  154. # Add functionality to show the Toplevel window from the menu bar
  155. def show_widget_window(widget):
  156.     widget.deiconify()
  157.     widget.lift()
  158.  
  159. root = tk.Tk()
  160. root.title("NoCode Tkinter Builder")
  161. root.geometry('+0+0')
  162.  
  163. canvas_width = 1280
  164. canvas_height = 560
  165. canvas = tk.Canvas(root, width=canvas_width, height=canvas_height, bg="white")
  166. canvas.pack()
  167.  
  168. widgets = []
  169. selected_widget = None
  170.  
  171. for idx, widget_type in enumerate(["button", "label", "entry"]):
  172.     x = 50 + (idx * 250)  # Adjusting x-coordinate for each widget type
  173.     widget = create_widget(widget_type, x, 50)
  174.     widget.place(x=x, y=50, width=150)  # Setting width to 200px
  175.     widgets.append(widget)
  176.     widget.bind("<Button-1>", lambda e: select_widget(e))
  177.     widget.bind("<Double-Button-1>", delete_widget)
  178.     widget.bind("<ButtonRelease-3>", lambda e: deselect_widgets())
  179.  
  180. # Creating menu bar and adding options to show the widgets control window
  181. menubar = tk.Menu(root)
  182. root.config(menu=menubar)
  183. widget_menu = tk.Menu(menubar, tearoff=0)
  184. menubar.add_cascade(label="Widgets", menu=widget_menu)
  185. widget_menu.add_command(label="Show Widgets Control", command=show_widgets_control)
  186.  
  187. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement