2016-01-23 151 views
0

我試圖用for循環創建帶有不確定數量的標籤的GUI。 標籤的數量應該可以通過按鈕觸發的func來改變。 這裏是我的代碼:for循環更新範圍按鈕

from tkinter import * 


def change1(): 
    print(days) 
    in1 = input('how many days?') 
    for a in range(0, len(days)): 
     days.remove(days[0]) 
    for a in range(0, int(in1)): 
     in2 = input('type in another day: ') 
     days.append(in2) 
    krofl = (days[0], '-', days[len(days)-1]) 
    firstlast.set(krofl) 

Fenster = Tk() 
Fenster.title("some title") 
Fenster.minsize(width=800, height=900) 
Fenster.resizable(width=FALSE, height=FALSE) 

presetdays = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'] 

days = [] 

for a in range(0, len(presetdays)): 
    days.append(presetdays[a]) 

firstlast = StringVar() 
firstlast.set(str("preset value")) 

lab1_1 = Label(Fenster, text='Zeitspanne:') 
lab1_1.grid(row=0, column=0) 

lab1_2 = Label(Fenster, textvariable = firstlast) 
lab1_2.grid(row=0, column=1) 

but1 = Button(Fenster, text='Change', command=change1) 
but1.grid(row=0, column=2) 


for a in range(0, len(days)): 
    karamel = Label(Fenster, text = days[a]) 
    karamel.grid(row=a, column=6) 

Fenster.mainloop() 

我的問題是,標貼的名單將在週一,週二.....即使按下按鈕&改變天列表之後。 我曾嘗試以下:

number_days = IntVar() 
number_days.set(7) 

和DEF CHANGE1添加此:

sth = int(len(days)) 
number_days.set(sth) 

和改變for循環到底這樣的:

for a in range(0, number_days): 
karamel = Label(Fenster, text = days[a]) 
karamel.grid(row=a, column=6) 

,但它返回錯誤: TypeError:'IntVar'對象不能被解釋爲整數

有什麼辦法讓這個工作,以便所有的標籤更新def def1()後?

回答

1

number_days不是正常的整型變量。它是對象IntVar。你必須使用number_days.get()


要改變你要記住karamel所有標籤的列表(例如all_labels),然後使用all_labels[number].config(text='new text')或all_labels [數字] [「文本」] =「新文本」標籤`

您還可以使用some_label.grid_forget()從網格中刪除標籤。

from tkinter import * 


def change_labels(): 
    print(days) 

    in1 = int(input('how many days?')) 

    # remove all old days 
    days.clear() 

    for a in range(in1): 
     in2 = input('type in another day: ') 
     days.append(in2) 

    krofl = (days[0], '-', days[-1]) 
    firstlast.set(krofl) 

    # it is simpler to remove all labels and create new labels 

    # remove all old labels 
    for l in labels:  
     l.grid_forget() 

    # create new labels 
    for number, name in enumerate(days): 
     karamel = Label(Fenster, text=name) 
     karamel.grid(row=number, column=6) 
     labels.append(karamel) 


# --- 

presetdays = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'] 

# copy list 
days = presetdays[:] 

# --- 

Fenster = Tk() 
Fenster.title("some title") 
Fenster.minsize(width=800, height=900) 
Fenster.resizable(width=FALSE, height=FALSE) 

firstlast = StringVar() 
firstlast.set(str("preset value")) 

lab1_1 = Label(Fenster, text='Zeitspanne:') 
lab1_1.grid(row=0, column=0) 

lab1_2 = Label(Fenster, textvariable = firstlast) 
lab1_2.grid(row=0, column=1) 

but1 = Button(Fenster, text='Change', command=change_labels) 
but1.grid(row=0, column=2) 

# --- create labels first time 

labels = [] 

for number, name in enumerate(days): 
    karamel = Label(Fenster, text=name) 
    karamel.grid(row=number, column=6) 
    labels.append(karamel) 

Fenster.mainloop() 
+0

您的意思是改變for循環的結束:在範圍(0表示,number_days.get()?...... –

+0

你嘗試它 – furas

+0

是它不返回錯誤但不更新標籤... –