0

我想从 ListBox 中的选定内容中删除数据。我尝试了一切,但它不起作用。我试过简单的文件。有效。我在这里写了相同的代码,但它不起作用。我被困两天了。请帮忙。

from tkinter import *
import sqlite3
from tkinter import messagebox
import randevuEkleme as fRE
from tkcalendar import *


def randevularFormunuAc():
    root = Tk()
    root.title("Randevu Listesi Formu")
    root.geometry("750x500")

    randevuListesi = Listbox(root, width=50, bg="yellow", selectmode=SINGLE)

    takvim = Calendar(root, selectmode="day", year=2020, month=5, day=22)
    takvim.grid(row=0, column=0)
    randevuListesi.grid(row=1, column=0)
    secilen = randevuListesi.curselection()

    def randevulariListele(): #OK!
        con = sqlite3.connect('veri2.db')
        c = con.cursor()
        c.execute("SELECT * FROM randevular")
        liste = c.fetchall()
        con.commit()
        con.close()
        randevuListesi.delete(first=0,last=END)
        for i in liste:
        randevuListesi.insert(END,i)

    con = sqlite3.connect('veri2.db')
    c = con.cursor()
    c.execute("SELECT * FROM randevular")
    liste = c.fetchall()
    con.commit()
    con.close()
    for i in liste:
        randevuListesi.insert(END, i)

    def randevuSil():  
        con = sqlite3.connect('veri2.db')
        c = con.cursor()

        # Problem is HERE!!!   
        c.execute("DELETE FROM randevular WHERE rowid = (?)", (secilen[0] + 1,))
        
        # PROBLEMIS HERE!!!
        con.commit()
        con.close()

    btnListele = Button(root, text="Randevuları Listele",command=randevulariListele)
    btnRandevuEkle = Button(root,text="Randevu Ekle",command=fRE.randevuEklemeFormunuAc)
    btnRandevuSil = Button(root,text="Randevuyu Sil",command=randevuSil()) !!!!
    btnCikis = Button(root, text="Geri", command=root.quit)
    btnListele.grid(row=2,column=0)
    btnRandevuEkle.grid(row=2,column=1)
    btnRandevuSil.grid(row=2,column=2)
    btnCikis.grid(row=2,column=3)

    root.mainloop()

错误信息是:

File "C:\Users\kutla\PycharmProjects\fizyoOta1.0\randevularFormu.py", line 43, in randevuSil
c.execute("DELETE FROM randevular WHERE rowid = (?)", (secilen[0] + 1,))
IndexError: tuple index out of range
4

1 回答 1

0

快速阅读的两种可能性:

  • 这条线secilen = randevuListesi.curselection()可能需要在randevuSil()而不是作为设置的一部分。
  • 设置按钮时,()应省略command=:
    btnRandevuSil = Button(root,text="Randevuyu Sil",command=randevuSil)
    
    否则,该函数将作为设置的一部分(当尚未选择任何内容时)而不是在单击按钮时调用。
于 2020-08-26T13:45:08.447 回答