RE:TKinter表行删除(PYTHON)

2024-10-03 09:19:12 发布

您现在位置:Python中文网/ 问答频道 /正文

(如果有人看到我前面的问题,我找到了保存行索引并在delete函数中使用它们的方法:3)

是否有方法通过单击按钮停止/删除正在运行的函数/表?我正在制作一个简单的客户注册程序,它保存文本字段(cIDcustNamecustEmailcustBday)中的数据并删除选定的行。我的问题是,每当我删除一行时,最后一行总会在删除一行时留下/落后于创建此副本。有没有什么建议,或者我做错了什么,或者我错过了什么

我的代码:

from tkinter import *
from tkinter import messagebox
import tkinter as tk

at = False
dotcom = False
list = [['ID', 'NAME', 'EMAIL', 'BIRTHDATE']]
recentIndex = []
ID = 1
storedIndex = 0


def callback(event):


    custid.config(state=NORMAL)
    custid.delete('1.0', END)
    custid.insert(1.0, list[event.widget._values][0])
    custid.config(state=DISABLED)

    cName.set(list[event.widget._values][1])
    cEmail.set(list[event.widget._values][2])
    birthdate.set(list[event.widget._values][3])
    indexPosition = event.widget._values
    recentIndex.append(indexPosition)
    print(recentIndex)

def createTable():
    for i in range(len(list)):
        for j in range(len(list[0])):
            mgrid = tk.Entry(window, width = 10, bg= 'yellow')
            mgrid.insert(tk.END, list[i][j])
            mgrid._values= i
            mgrid.grid (row = i + 5,column = j + 5)
            mgrid.bind("<Button-1>", callback)


def delete():
    try:
        list.pop(recentIndex[-1])
        if recentIndex[-1] > 0 or NONE:
            msgbox("Data Delete", "Record")
            createTable()
        else:
            msgbox("No Data Selected", "record")
            return
    except:
        msgbox("No Data Selected", "record")

def save ():
    
        custid.config(state= NORMAL)
        initialValue = int(custid.get('1.0', END))
        custid.delete('1.0', END)
        list.append([ID, custName.get(), custEmail.get(), custBday.get()])
        custid.insert(1.0, initialValue + 1)
        custid.config(state=DISABLED)
        createTable()

        msgbox("Saved", "Record")


        ID = ID + 1


def msgbox (msg, titlebar):
    messagebox.showinfo(title = titlebar, message=msg)

def products():
    window = Tk()
    window.title("Products Form")
    window.geometry("550x400")
    window.configure(bg="orange")



window = Tk()
window.title("Sample Window")
window.geometry("550x400")
window.configure(bg="orange")

menubar = Menu(window)
filemenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Products", command=products)
filemenu.add_command(label="Orders")
filemenu.add_separator()
filemenu.add_command(label="Close", command = window.quit)

window.config(menu=menubar)

labelTitle = Label(window, text="Customer Registration System", width=30, height=1, bg="yellow", anchor= "center")
labelTitle.config(font=("Courier", 10))
labelTitle.grid(column=2, row=1)

labelID = Label(window, text="Customer ID", width = 20, height = 1, bg = "yellow")
labelID.grid(column=1, row=2)

cID = StringVar()
custid = Text(window, width=15, height=1)
custid.grid(column=2, row=2)
custid.insert(1.0, "1")
custid.config(state = DISABLED)

labelNameCheck = Label(window, text="Last Name, First Name", width = 20, height = 1, bg = "yellow")
labelNameCheck.grid(column=3, row=3)
labelName = Label(window, text="Customer Name", width = 20, height = 1, bg = "yellow")
labelName.grid(column=1, row=3)

cName = StringVar()
custName = Entry(window, textvariable = cName)
custName.grid(column=2, row=3)

labelEmail = Label(window, text="Customer Email", width = 20, height = 1, bg = "yellow")
labelEmail.grid(column=1, row=4)


cEmail = StringVar()
custEmail = Entry(window, textvariable = cEmail)
custEmail.grid(column=2, row=4)
custEmail.bind("<KeyRelease>", keyup)

labelEmail = Label(window, text="[a-z]@[a-z].com", width = 20, height = 1, bg = "yellow")
labelEmail.grid(column=3, row=4)

labelBday = Label(window, text="Customer Birthdate", width = 20, height = 1, bg = "yellow")
labelBday.grid(column=1, row=5)

birthdate= StringVar()
custBday = Entry(window, textvariable = birthdate)
custBday.grid(column=2, row=5)
custBday.bind("<KeyRelease>", keyupBdate)


birthdateCheck = Label(window, text="mm/dd/yyyy", width = 20, height = 1, bg = "yellow")
birthdateCheck.grid(column=3, row=5)

savebtn = Button(text = "Save", command = save)
savebtn.grid(column=1)


deletebtn = Button(text = "Delete", command = delete)
deletebtn.grid(column=1)



window.mainloop()

Tags: textidconfigcolumnwindowwidthlabelcommand
1条回答
网友
1楼 · 发布于 2024-10-03 09:19:12

您需要先删除当前显示的记录,然后才能在createTable()中显示更新的记录。另外,最好创建一个显示记录的框架,以便更容易清除显示的记录:

def createTable():
    # clear current displayed records
    for w in tableFrame.winfo_children():
        w.destroy()
    # populate records
    for i in range(len(list)):
        for j in range(len(list[0])):
            mgrid = tk.Entry(tableFrame, width = 10, bg= 'yellow')  # use tableFrame as parent
            mgrid.insert(tk.END, list[i][j])
            mgrid._values= i
            mgrid.grid (row = i,column = j)
            mgrid.bind("<Button-1>", callback)

...

# frame for showing records
tableFrame = Frame(window)
tableFrame.grid(row=5, column=5)

...

相关问题 更多 >