Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_nocode_2024.py ZZZ
- import tkinter as tk
- import tkinter.colorchooser as colorchooser
- def create_widget(widget_type, x, y):
- if widget_type == "button":
- return tk.Button(root, text="Button", width=10, height=2)
- elif widget_type == "label":
- return tk.Label(root, text="Label", bg="lightgrey", relief="raised")
- elif widget_type == "entry":
- return tk.Entry(root, width=15)
- else:
- raise ValueError("Invalid widget type")
- def add_widget(widget_type, x, y):
- widget = create_widget(widget_type, x, y)
- widget.place(x=x, y=y)
- widgets.append(widget)
- def delete_widget(event):
- widget = event.widget
- widget.destroy()
- widgets.remove(widget)
- def select_widget(event):
- global selected_widget
- selected_widget = event.widget
- for widget in widgets:
- widget.bind("<Button-1>", lambda e: select_widget(e))
- def deselect_widgets():
- for widget in widgets:
- widget.bind("<Button-1>", lambda e: select_widget(e))
- def change_text():
- if selected_widget:
- text = text_entry.get()
- selected_widget.configure(text=text)
- def bring_to_front():
- if selected_widget:
- selected_widget.lift()
- def send_to_back():
- if selected_widget:
- selected_widget.lower()
- def create_countdown_widget():
- countdown_label = tk.Label(root, text="Countdown Timer:", font=("Arial", 12))
- countdown_label.place(x=50, y=100)
- def show_properties():
- if selected_widget:
- properties_window = tk.Toplevel(root)
- properties_window.title("Widget Properties")
- properties_label = tk.Label(properties_window, text=f"Selected Widget: {type(selected_widget).__name__}")
- properties_label.pack()
- def create_hyperlink():
- hyperlink_label = tk.Label(root, text="Click here!", fg="blue", cursor="hand2")
- hyperlink_label.pack()
- hyperlink_label.bind("<Button-1>", lambda event: print("Hyperlink clicked!"))
- def upload_image():
- upload_button = tk.Button(root, text="Upload Image")
- upload_button.pack()
- def toggle_snap_to_grid():
- # Assuming grid snapping is needed for widget placement
- canvas.bind("<Button-1>", lambda event: snap_to_grid(event))
- def snap_to_grid(event):
- x = round(event.x / 10) * 10
- y = round(event.y / 10) * 10
- create_widget("button", x, y) # Create a button at snapped coordinates
- def apply_font_properties():
- if selected_widget:
- font_style = "Arial"
- font_size = 12
- font_color = "black"
- selected_widget.config(font=(font_style, font_size), fg=font_color)
- def pick_color():
- color = tk.colorchooser.askcolor()
- return color
- def apply_group_expand_fill(action):
- if selected_widget:
- if action == "group":
- print("Grouping widgets...")
- elif action == "expand":
- print("Expanding widgets...")
- elif action == "both":
- print("Filling both directions...")
- elif action == "x":
- print("Filling in X direction...")
- elif action == "y":
- print("Filling in Y direction...")
- def apply_text_alignment(alignment):
- text_area.tag_configure("align", justify=alignment)
- text_area.tag_add("align", "1.0", "end")
- def toggle_fullscreen():
- root.attributes("-fullscreen", not root.attributes("-fullscreen"))
- def show_widgets_control():
- global widget_window
- try:
- widget_window.destroy()
- except:
- pass
- def create_button(text, command=None):
- button = tk.Button(widget_window, text=text, command=command, width=25)
- button.config(width=50)
- button.pack()
- widgets_in_window.append(button)
- widget_window = tk.Toplevel(root)
- widget_window.title("Widget Window")
- widgets_in_window = []
- # Change Text section
- text_entry_label = tk.Label(widget_window, text="Change Text:")
- text_entry_label.pack()
- text_entry = tk.Entry(widget_window)
- text_entry.pack(fill='x')
- create_button("Apply Text Change", change_text)
- create_button("Bring to Front", bring_to_front)
- create_button("Send to Back", send_to_back)
- create_button("Add Countdown Timer", create_countdown_widget)
- create_button("Show Widget Properties", show_properties)
- create_button("Toggle Fullscreen", toggle_fullscreen)
- create_button("Create Hyperlink", create_hyperlink)
- create_button("Upload Image", upload_image)
- create_button("Toggle Snap to Grid", toggle_snap_to_grid)
- for alignment in ["Align Left", "Align Center", "Align Right"]:
- create_button(alignment, lambda a=alignment: apply_text_alignment(a.lower()))
- create_button("Apply Font Properties", apply_font_properties)
- create_button("Pick Color", pick_color)
- for action in ["Group", "Expand", "Fill (Both)", "Fill (X)", "Fill (Y)"]:
- create_button(action, lambda a=action.lower(): apply_group_expand_fill(a.lower()))
- widget_window.lift()
- # Add functionality to show the Toplevel window from the menu bar
- def show_widget_window(widget):
- widget.deiconify()
- widget.lift()
- root = tk.Tk()
- root.title("NoCode Tkinter Builder")
- root.geometry('+0+0')
- canvas_width = 1280
- canvas_height = 560
- canvas = tk.Canvas(root, width=canvas_width, height=canvas_height, bg="white")
- canvas.pack()
- widgets = []
- selected_widget = None
- for idx, widget_type in enumerate(["button", "label", "entry"]):
- x = 50 + (idx * 250) # Adjusting x-coordinate for each widget type
- widget = create_widget(widget_type, x, 50)
- widget.place(x=x, y=50, width=150) # Setting width to 200px
- widgets.append(widget)
- widget.bind("<Button-1>", lambda e: select_widget(e))
- widget.bind("<Double-Button-1>", delete_widget)
- widget.bind("<ButtonRelease-3>", lambda e: deselect_widgets())
- # Creating menu bar and adding options to show the widgets control window
- menubar = tk.Menu(root)
- root.config(menu=menubar)
- widget_menu = tk.Menu(menubar, tearoff=0)
- menubar.add_cascade(label="Widgets", menu=widget_menu)
- widget_menu.add_command(label="Show Widgets Control", command=show_widgets_control)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement