2016-05-31 31 views
0

我想創建一個提醒,通過按下確認按鈕來提醒我在組合框中選擇的時間。我把時間放在一個標籤中,並創建一個刪除按鈕,可以通過循環刪除同一行中的標籤和按鈕本身。它適用於只有一個標籤的情況,但是如果我增加了它的數量,它只能銷燬最後一個標籤和按鈕。下面Tkinter銷燬按鈕創建一個循環

是我的代碼:

class final: 
    def __init__(self,app): 
     self.savelist=[] 

     self.time= StringVar() 
     self.timecombo = ttk.Combobox(app,textvariable=self.time) 
     self.timecombo.grid(row=0,column=1) 
     self.timecombo.config(value =('1:00','2:00','3:00','4:00','5:00','6:00','7:00','8:00','9:00','10:00','11:00','12:00')) 

     self.button1=Button(app,text='confirmed',command=self.save) 
     self.button1.grid(row=3,column=2) 


    ***def save(self): 
     savetext = self.time.get() 
     self.savelist.append(savetext) 
     self.deletebutton_list = [] 
     self.savelabel_list = [] 
     for i in range(len(self.savelist)): 
      savelabel = Label(app, text=self.savelist[i]) 
      savelabel.grid(row=4 + i, column=0) 
      self.savelabel_list.append((savelabel)) 
      deletebutton = Button(app, text='delete' , command=functools.partial(self.deletelabel,idx=i)) 
      deletebutton.grid(row=4 + i, column=1) 
      self.deletebutton_list.append(deletebutton) 
    def deletelabel(self, idx): 
     self.savelabel_list[idx].destroy() 
     self.deletebutton_list[idx].destroy() 
     self.savelist.remove(self.savelist[idx]) 
     self.savelabel_list.remove(self.savelabel_list[idx]) 
     self.deletebutton_list.remove(self.deletebutton_list[idx])*** 

app = Tk() 
a = final(app) 
app.title('things to do') 
app.geometry("500x300+200+200") 

app.mainloop() 

我認爲,必須有一些錯誤的循環或功能deletelabel,但我仍然無法修復它。

回答

0
self.savelabel_list.remove(self.savelabel_list[idx]) 

不要更改列表。如果刪除標籤/按鈕#1,則標籤#2變爲#1,因此當您按下按鈕刪除標籤#2時,它會刪除標籤#3,因爲所有內容都已上移。此外,請注意,每次調用「save()」時,它都會創建一組覆蓋舊窗口小部件的新窗口小部件,這最終會降低計算機的速度。僅創建併網格化新時間標籤。使用self.next_row變量(或其他)跟蹤該行並每次增加一個。

+0

謝謝您的回答,但即使我刪除您推薦之列,我仍不能達到我的目標,我不知道如何只箱子和電網新的時間標籤,因爲我是Python新手。你能爲我寫代碼嗎?謝謝。 –

+0

,我想我已經通過使用functools.partial解決了您提到的問題,因爲我可以在單擊它後看到列表中標籤/按鈕的正確順序。 –

0

這是一個問題,指出如何使用類。爲每個提醒創建一個新類,帶有標籤和關閉按鈕。

from tkinter import * 
from tkinter import ttk 

class NewButton: 
    def __init__(self, master, label_text, this_row): 
     ## put everything in a new frame so destroying 
     ## one frame will destroy everything in it 
     self.fr=Frame(master) 
     self.fr.grid(row=this_row, column=1) 

     Label(self.fr, text=label_text).grid(row=0, column=1) 
     Button(self.fr, text="Close This", 
       command=self.fr.destroy).grid(row=0, column=2) 

class Final: 
    def __init__(self,app): 
     self.app=app 
     self.this_row=4 

     self.time_var= StringVar() 
     self.timecombo = ttk.Combobox(app,textvariable=self.time_var) 
     self.timecombo.grid(row=0,column=1) 

     self.button1=Button(app,text='confirmed',command=self.save) 
     self.button1.grid(row=3,column=2) 


    def save(self): 
     save_text = self.time_var.get() 
     self.this_row += 1 
     next_button=NewButton(self.app, save_text, self.this_row) 
     self.time_var.set("") 

app = Tk() 
a = Final(app) 
app.title('things to do') 
app.geometry("500x300+200+200") 

app.mainloop()