import Tkinter
import os
from os.path import isfile, isdir, join
Ymypath = "%s/input/" % (os.getcwd())
Ydirs = [d for d in os.listdir(Ymypath) if isdir(join(Ymypath, d))]
# Mpath = "%s/input/%s/" % (os.getcwd(),Yvariable.get())
# Mdirs = [d for d in os.listdir(Mpath) if isdir(join(Mpath, d))]
def update_mlist(curvalue):
Y = curvalue
# Rewrite the Menubutton associated with the Optionmenu.
menu0 = toption0['menu']
menu0.delete(0, 'end')
menu = toption['menu']
menu.delete(0, 'end')
Mpath = "%s/input/%s/" % (os.getcwd(),Yvariable.get())
Mdirs = [d for d in os.listdir(Mpath) if isdir(join(Mpath, d))]
for n in range(len(Mdirs)):
menu0.add_command(label=Mdirs[n], command=Tkinter._setit(targetM, Mdirs[n]))
Mpath = "%s/input/%s/" % (os.getcwd(),curvalue)
Mdirs = [d for d in os.listdir(Mpath) if isdir(join(Mpath, d))]
targetM.set(Mdirs[0])
Fpath = "%s/input/%s/%s" % (os.getcwd(),Yvariable.get(),targetM.get())
onlyfiles = [f for f in os.listdir(Fpath) if isfile(join(Fpath, f))]
onlyfilesN = sorted([int(f.split('-')[1]) for f in onlyfiles])
for n in range(len(onlyfilesN)):
menu.add_command(label=onlyfilesN[n], command=Tkinter._setit(targetD, onlyfilesN[n]))
Fpath = "%s/input/%s/%s/" % (os.getcwd(),Y,targetM.get())
onlyfiles = [f for f in os.listdir(Fpath) if isfile(join(Fpath, f))]
onlyfilesN = sorted([int(f.split('-')[1]) for f in onlyfiles])
targetD.set(onlyfilesN[0])
def update_dlist(val):
menu = toption['menu']
menu.delete(0, 'end')
Fpath = "%s/input/%s/%s" % (os.getcwd(),Yvariable.get(),targetM.get())
print Fpath
onlyfiles = [f for f in os.listdir(Fpath) if isfile(join(Fpath, f))]
onlyfilesN = sorted([int(f.split('-')[1]) for f in onlyfiles])
for n in range(len(onlyfiles)):
menu.add_command(label=onlyfilesN[n], command=Tkinter._setit(targetD, onlyfilesN[n]))
Fpath = "%s/input/%s/%s/" % (os.getcwd(),Yvariable.get(),val)
onlyfiles = [f for f in os.listdir(Fpath) if isfile(join(Fpath, f))]
onlyfilesN = sorted([int(f.split('-')[1]) for f in onlyfiles])
if len(onlyfilesN)>0:
targetD.set(onlyfilesN[0])
master = Tkinter.Tk()
Tkinter.Label(text=u"Year: ").grid(row=0, column=0)
Yvariable = Tkinter.StringVar(value='2017')
coption = Tkinter.OptionMenu(master, Yvariable, *Ydirs, command=update_mlist)
coption.grid(row=0,column=1)
Tkinter.Label(text=u"Month: ").grid(row=1, column=0)
Mpath = "%s/input/%s/" % (os.getcwd(),Yvariable.get())
Mdirs = [d for d in os.listdir(Mpath) if isdir(join(Mpath, d))]
targetM = Tkinter.StringVar(value=Mdirs[0])
toption0 = Tkinter.OptionMenu(master, targetM, *Mdirs, command=update_dlist)
toption0.grid(row=1, column=1)
Tkinter.Label(text=u"Day: ").grid(row=2, column=0)
Fpath = "%s/input/%s/%s" % (os.getcwd(),Yvariable.get(),targetM.get())
onlyfiles = [f for f in os.listdir(Fpath) if isfile(join(Fpath, f))]
onlyfilesN = sorted([int(f.split('-')[1]) for f in onlyfiles])
if len(onlyfilesN)>0:
targetD = Tkinter.StringVar(value=onlyfilesN[0])
else:
targetD = Tkinter.StringVar(value='')
toption = Tkinter.OptionMenu(master, targetD, *onlyfilesN)
toption.grid(row=2, column=1)
master.mainloop()
天数列表未正确更新, 因为年份没有更新。
我尝试在Tkinter中绑定3个下拉菜单。
没有错误消息, 但是非默认年份的天数列表不会更新。
嗨@mmgp,你知道怎么解决吗?请帮帮我。
任选地:
将tkinter日历实现为选择年,月和日(周末大部分时间禁用,以及某些指定的工作日)也会很有趣。
答案 0 :(得分:1)
您可以正确定义Fpath两次(使用get()
)并且错误地定义:
Fpath = "%s/input/%s/%s/" % (os.getcwd(),targetM,curvalue)
您需要将其更改为:
Fpath = "%s/input/%s/%s/" % (os.getcwd(),targetM.get(),curvalue)
让这成为重用变量名称的教训。