1
我有下面的代碼,我想將窗口小部件初始化爲默認值,但他們堅持不這樣做。我設置了StringVar()變量,我正在分配它們,但它仍然不起作用。有什麼建議麼?在Toplevel()窗口中設置默認值
import tkinter as tk
from tkinter import ttk
class popup():
def __init__(self):
edit_window=tk.Toplevel()
works_checkbutton_val=[tk.IntVar()]*7
start_time_combo_val=[tk.StringVar()]*7
end_time_combo_val=[tk.StringVar()]*7
datapoint=['Joe Blow','8 AM to 5 PM','8 AM to 5 PM','8 AM to 5 PM','8 AM to 5 PM','8 AM to 5 PM','8 AM to 5 PM','8 AM to 5 PM','3foobarIID']
headings =['Employee Name','Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
for row in range(1,len(datapoint)-1):
ttk.Label(edit_window, text=headings[row]).grid(row=row+1, column=0, sticky='e', pady=((7.5,0),(7.5,7.5))[row==len(datapoint)-2], padx=5)
ttk.Label(edit_window, text=headings[0]).grid(row=0, column=0, sticky='e', pady=(7.5,0), padx=5)
employee_name_entry = ttk.Entry(edit_window)
employee_name_entry.insert(0, datapoint[0])
employee_name_entry.grid(row=0, column=1, columnspan=4, pady=(7.5,0), padx=5, sticky='ew')
works_checkbutton=[]
start_time_combo=[]
end_time_combo=[]
for row in range(1,len(datapoint)-1):
split_time=datapoint[row].split(' to ')
if len(split_time)==1:
works_checkbutton_val[row-1].set(0)
start_time_combo_val[row-1].set('0 AM')
end_time_combo_val[row-1].set('0 AM')
else:
works_checkbutton_val[row-1].set(1)
start_time_combo_val[row-1].set(split_time[0])
end_time_combo_val[row-1].set(split_time[1])
works_checkbutton.append(ttk.Checkbutton(edit_window, variable = works_checkbutton_val[row-1]))
works_checkbutton[row-1].grid(row=row+1, column=1, padx=2.5, pady=(0,(0,7.5))[row==len(datapoint)-2], sticky='s')
start_time_combo.append(ttk.Combobox(edit_window, values=["%s AM"%(x+1) for x in range(12)] + ["%s PM"%(x+1) for x in range(12)],textvariable=start_time_combo_val[row-1]))
start_time_combo[row-1].grid(row=row+1, column=4, pady=((7.5,0),(7.5,7.5))[row==len(datapoint)-2], padx=5)
end_time_combo.append(ttk.Combobox(edit_window, values=["%s AM"%(x+1) for x in range(12)] + ["%s PM"%(x+1) for x in range(12)],textvariable=end_time_combo_val[row-1]))
end_time_combo[row-1].grid(row=row+1, column=3, pady=((7.5,0),(7.5,7.5))[row==len(datapoint)-2], padx=5)
main_window=tk.Tk()
b1 = ttk.Button(main_window,text='here',command=lambda:popup())
b1.pack()
main_window.mainloop()
需要'[tk.IntVar()對於x範圍內(7)] [)tk.StringVar(有效範圍內(7)X]''' – furas
謝謝,這解決了另一個問題,我所有選項都被鏈接。但我仍然沒有獲得初始值。 – Vedvart1