首页 > 解决方案 > Tkinter 拖放标签信息到数据库

问题描述

我创建了一个简单的 GUI 来查看一个人坐在哪个座位上。名称标签可以拖放到不同的静态座位标签上。每次有人更换座位时,用户都可以将姓名标签拖放到不同的座位上。

我希望能够将该信息发送到数据库(无论是 excel 还是 sql 都可以),然后我可以稍后查看并找出谁坐在哪里等。我尝试过使用鼠标的坐标,但不够准确。有一个更好的方法吗?也许与widget.grid?

谢谢![在此处输入图片说明][1]

这是我的代码;

def label_maker(Seat_list,my_frame1, my_frame2):
    run_lisst_title_label1 = Label(my_frame1,text = "Seat",compound='center',bd=2,relief=SUNKEN,bg=title_label_bgcolour,fg="white",font = font,width=12)
    run_lisst_title_label1.place(x=7,y=50)
    y_loco=90
    x_loco=7

    for item in Seat_list[0:25]:
        run_label = Label(my_frame1,text = item,compound='center',bd=2,relief=SUNKEN,bg=seat_label_colour,fg="white",font = font,width=12)
        run_label.place(x=x_loco,y=y_loco)
        y_loco = y_loco + y_loco_plus
    
def drag_drop(name_list,my_frame2):

    consist_list_title_label1 = Label(my_frame2,text = "Name",compound='center',bd=2,relief=SUNKEN,bg=title_label_bgcolour,fg="white",font = font,width=12)
    consist_list_title_label1.place(x=7,y=50)


    def drag_start(event):
        widget = event.widget
        widget.startX = event.x
        widget.startY = event.y

    def drag_motion(event):
        widget = event.widget
        x = widget.winfo_x() - widget.startX + event.x
        y = widget.winfo_y() - widget.startY + event.y
        widget.place(x=x//5*5, y=y//5*5)

    y_loco=90
    for item in name_list[0:25]:
            x_loco=7
            lrv_label = Label(my_frame2,text = item,compound='center',bd=1,relief=SUNKEN,bg=lrv_colour,fg="white",font = font )
            lrv_label.place(x=x_loco,y=y_loco)
            lrv_label.bind("<Button-1>",drag_start)
            lrv_label.bind("<B1-Motion>",drag_motion)
            y_loco = y_loco +y_loco_plus

    y_loco=90

[enter image description here][1]

标签: pythondatabasetkinterdrag-and-drop

解决方案


推荐阅读