lvcpp
lvcpp

Reputation: 169

fix second callback for change event in Tkinter OptionMenu

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()

the list of Days does not update correctly, because Year does not update.

I try to bound 3 drop-down menus in Tkinter.

There no error messages, but the list of days does not update for non default year.

hi @mmgp , do you know how to fix it? please help me.

Optionally:

Also would be interesting to implement it as tkinter calendar to select year, month and day(with disabled most of weekends, and some specified work days).

Upvotes: 0

Views: 117

Answers (1)

Novel
Novel

Reputation: 13729

You define Fpath twice, once correctly (using get()) and once incorrectly:

Fpath = "%s/input/%s/%s/" % (os.getcwd(),targetM,curvalue)

You need to change that to this:

Fpath = "%s/input/%s/%s/" % (os.getcwd(),targetM.get(),curvalue)

And let that be a lesson to you about reusing variable names.

Upvotes: 1

Related Questions