文本框不会移动

huangapple go评论66阅读模式
英文:

Textbox won't move

问题

我编写了一个程序,其中你可以添加文本框和图像。当你点击文本框并悬停在上面时,会显示两个按钮:Bin(删除)Move(移动)

Bin(删除)按钮: 点击它会删除文本框。

Move(移动)按钮: 长按点击它可以移动文本框。

到目前为止,删除按钮可以正常工作,但移动按钮不起作用。长按点击它时,文本框不会移动。有关我做错了什么的任何想法吗?

我对Python还相对新,所以任何建议都非常受欢迎!

import tkinter as tk
from tkinter import filedialog
from PIL import ImageTk, Image

# 以下是你的代码的剩余部分...

你可以检查一下代码中关于移动按钮的事件处理函数,确保它们正确绑定并响应。如果问题仍然存在,可以提供更多细节,以便我能够提供更具体的帮助。

英文:

I coded a program where you can add text boxes and images. When you click and hover on the textbox it shows two buttons: Bin and Move.

BIN BUTTON: Clicking on it deletes the textbox.

MOVE BUTTON: Hold clicking it allows you to move the text box around.

So far the bin button works, but not the move button. When hold clicking it won't move the textbox around. Any ideas on what I'm doing wrong?

I'm still fairly new to Python so any advice is more than welcome!

import tkinter as tk
from tkinter import filedialog
from PIL import ImageTk, Image

root = tk.Tk()
root.withdraw()

def save_file():
    file_path = filedialog.asksaveasfilename(defaultextension=".leaff", filetypes=[("Leaf Files", "*.leaff")])
    if file_path:
        # Perform saving logic here
        # Update the window title with the saved file name
        new_window.title(file_path)

def save_as_file():
    file_path = filedialog.asksaveasfilename(filetypes=[("Leaf Files", "*.leaff")])
    if file_path:
        # Perform saving logic here
        # Update the window title with the saved file name
        new_window.title(file_path)

def load_new_branch():
    file_path = filedialog.askopenfilename(filetypes=[("Leaf Files", "*.leaff")])
    if file_path:
        # Perform loading logic here
        # Update the window title with the loaded file name
        new_window.title(file_path)

def create_new_branch():
    window.withdraw()  # Hide the main menu window

    # Create a new window
    global new_window
    new_window = tk.Toplevel(root)
    new_window.geometry("700x400")
    new_window.title("New Branch")

    def add_text():
        text_entry = tk.Entry(new_window, bd=0)
        text_entry.pack(pady=2)

        def on_entry_hover(event):
            text_entry.config(highlightbackground="black", highlightcolor="black", highlightthickness=2)

        def on_entry_leave(event):
            text_entry.config(highlightbackground="", highlightcolor="", highlightthickness=0)

        def on_entry_click(event):
            on_entry_hover(event)
            delete_button.place(x=text_entry.winfo_x() + text_entry.winfo_width() - 20,
                               y=text_entry.winfo_y() + text_entry.winfo_height() + 5)
            move_button.place(x=delete_button.winfo_x() - 35,
                              y=delete_button.winfo_y())

        text_entry.bind("<Enter>", on_entry_hover)
        text_entry.bind("<Leave>", on_entry_leave)
        text_entry.bind("<Button-1>", on_entry_click)

        # Create the delete button
        def delete_text():
            text_entry.destroy()
            delete_button.destroy()
            move_button.destroy()

        delete_button = tk.Button(new_window, image=delete_icon, command=delete_text)
        delete_button.image = delete_icon

        # Create the move button
        def move_text(event):
            def on_drag_start(event):
                text_entry._drag_data = {'x': event.x, 'y': event.y}

            def on_drag_motion(event):
                delta_x = event.x - text_entry._drag_data['x']
                delta_y = event.y - text_entry._drag_data['y']
                new_x = text_entry.winfo_x() + delta_x
                new_y = text_entry.winfo_y() + delta_y
                text_entry.place(x=new_x, y=new_y)
                text_entry._drag_data = {'x': event.x, 'y': event.y}

            def on_drag_release(event):
                text_entry.unbind('<B1-Motion>')
                text_entry.unbind('<ButtonRelease-1>')

            text_entry.bind('<ButtonPress-1>', on_drag_start)
            text_entry.bind('<B1-Motion>', on_drag_motion)
            text_entry.bind('<ButtonRelease-1>', on_drag_release)

        move_button = tk.Button(new_window, image=move_icon)
        move_button.image = move_icon
        move_button.bind('<ButtonPress-1>', move_text)

    def add_image():
        # Add image logic here
        print("Image added")

    def return_to_main_menu():
        new_window.destroy()  # Close the branch window
        window.deiconify()  # Show the main menu window

    # Create the file menu
    file_menu = tk.Menu(new_window)
    file_menu.add_command(label="Save", command=save_file)
    file_menu.add_command(label="Save As", command=save_as_file)
    file_menu.add_command(label="Exit", command=return_to_main_menu)

    # Create the add menu
    add_menu = tk.Menu(new_window)
    add_menu.add_command(label="Text", command=add_text)
    add_menu.add_command(label="Image", command=add_image)

    # Create the main menu bar
    menu_bar = tk.Menu(new_window)
    menu_bar.add_cascade(label="File", menu=file_menu)
    menu_bar.add_cascade(label="Add", menu=add_menu)
    new_window.config(menu=menu_bar)

# Create the main window
window = tk.Toplevel(root)
window.geometry("700x400")
window.title("Leaf")

# Load the background image
original_image = Image.open("images/Leaf.jpg")
resized_image = original_image.resize((700, 400), Image.ANTIALIAS)  # Resize to window dimensions
background_image = ImageTk.PhotoImage(resized_image)

# Create a label with the background image
background_label = tk.Label(window, image=background_image)
background_label.place(relx=0, rely=0, relwidth=1, relheight=1)

button_width = 20

# Create the "Create new branch" button
create_button = tk.Button(window, text="Create new branch", command=create_new_branch, width=button_width)
create_button.place(relx=0.64, rely=0.375)

# Create the "Load new branch" button
load_button = tk.Button(window, text="Load new branch", command=load_new_branch, width=button_width)
load_button.place(relx=0.64, rely=0.475)

# Load the delete button image
delete_icon = ImageTk.PhotoImage(Image.open("images/bin.png"))

# Load the move button image
move_icon = ImageTk.PhotoImage(Image.open("images/move.png"))

window.mainloop()

enter image description here

答案1

得分: 0

您的代码在大部分情况下对我有用,除了移动有点不稳定。这里是一个更新的 on_drag_motion 方法,修复了移动问题。

def on_drag_motion(event):
    widget = event.widget
    x = widget.winfo_x() - widget._drag_data['x'] + event.x
    y = widget.winfo_y() - widget._drag_data['y'] + event.y
    widget.place(x=x, y=y)

另外,请确保您点击 Move 按钮,然后拖动文本框到您想要的位置。

希望这有所帮助!

英文:

Your code worked mostly for me, except the movement was janky. Here's an updated on_drag_motion method that fixes the movement.

def on_drag_motion(event):
    widget = event.widget
    x = widget.winfo_x() - widget._drag_data['x'] + event.x
    y = widget.winfo_y() - widget._drag_data['y'] + event.y
    widget.place(x=x, y=y)

Also, make sure you click the Move button, but then drag the text box to wherever you want it to go.

Hope this helps!


Edit: Streamable demo video


Edit after OP comment.

def move_text(event):
    def on_drag_start(event):
        move_button._drag_data = {'x': event.x, 'y': event.y}

    def on_drag_motion(event):
        widget = event.widget
        x = widget.winfo_x() - widget._drag_data['x'] + event.x
        y = widget.winfo_y() - widget._drag_data['y'] + event.y


        tx = text_entry.winfo_x() - widget._drag_data['x'] + event.x
        ty = text_entry.winfo_y() - widget._drag_data['y'] + event.y
        widget.place(x=x, y=y)
        text_entry.place(x=tx, y=ty)

    def on_drag_release(event):
        move_button.unbind('<B1-Motion>')
        move_button.unbind('<ButtonRelease-1>')

    on_drag_start(event)
    move_button.bind('<B1-Motion>', on_drag_motion)
    move_button.bind('<ButtonRelease-1>', on_drag_release)

move_button = tk.Button(new_window, text='move')
move_button.image = move_icon
move_button.bind('<ButtonPress-1>', move_text)

This should allow you to hold the move button and drag, and the text entry will move with the mouse.

huangapple
  • 本文由 发表于 2023年6月19日 20:26:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506617.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定