如何在没有按钮的情况下更新tkinter中的选项菜单?

时间:2019-11-07 18:00:39

标签: python-3.x tkinter

您好,我是tkinter的新手,我只是在学校为我的计算机科学课程做一些实验,我想创建一个用户可以输入假期的系统,然后将其显示在可视日历上。

当前,如果选择了某些月份,我希望能够更改日期选项菜单中显示的天数。就像2月只有29天(在大多数情况下)一样。

我不知道该怎么做,将不胜感激。

我尝试过在线查找解决方案,但似乎找不到适合我的解决方案。

from tkinter import *
import time
master = Tk() #sets a variable for tkinter main variable
master.title("Enter Your Holiday") #This shows the title for the window

month31 = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"]
month30 = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"]

filename = "dates.txt"
file = open(filename,"w") #opens a file called dates.txt for writing

Mvar = StringVar(master)
Mvar.set("January") # initial value for month
Dvar = StringVar(master)
Dvar.set("1st") # initial value for date
Yvar = StringVar(master)
Yvar.set("2019") # initial value for year

s_month = month31
current = "January"

month = OptionMenu(master, Mvar, "January", "February", "March", "April","May","June","July","August","September","October","November","December")
month.pack() #creates the optionmenu button for months

date = OptionMenu(master, Dvar, *s_month)
date.pack() #creates the optionmenu button for dates


year = OptionMenu(master, Yvar, "2019","2020","2021")
year.pack() #creates the optionmenu button for years
def change_dropdown(*args): #prints current selected option
    print(Mvar.get())
    current = Mvar.get
Mvar.trace('w',lambda *args: change_dropdown) #selects current chosen option, lambda is ??

if current == "January" or "March" or "May" or "July" or "August" or "October" or "December":
    s_month = month31 #attempts at changing list for the optionmenu
else:
    s_month = month30

我希望能够在不需要刷新按钮的情况下进行更新,并且在运行代码时什么也没发生。

1 个答案:

答案 0 :(得分:0)

您的选项菜单似乎设置不正确

类似这样的东西:

import tkinter as tk  #I like to avoid the wildcard import

month = StringVar()
monthList = [
             'pick a month',
             'Jan', 'Feb', 'Mar', 'Apr',
             'May', 'Jun', 'Jul', 'Aug',
             'Sep', 'Oct', 'Nov', 'Dec'
            ]

def MonthSelEvent(*args):
    if month.get() =='Feb':
        pass #feb stuff here, pass is placeholder
    elif month.get()=='Jan' #extend with 'or' to include all 31 day months
        pass #31 day month stuff here, pass is placeholder
    elif month.get()=='Apr' #extend with 'or' to include all 30 day months
        pass #30 day month stuff here, pass is placeholder
    else:
        pass #covers the 'select a month' option

monthSelect = tk.OptionMenu(master, month, *monthList, command = MonthSelEvent)
month.trace("w", MonthSelEvent) #trace on the variable that is changed
#trace triggers call on "w"
month.set(monthList[0]) #set initial value of dropdown


#to update the menu values for days 
#this would go inside the 31 day month elif
date['menu'].delete(0, 'end')
for x in month31:
    date['menu'].add_command(label = x, command = tk._setit(Dvar, x))
Dvar.set(month31[0]) #set it to first/default for selection