1 year ago

#317000

test-img

AmarN

Tracing changes in Optionmenu outputting error (tkinter)

I am trying to detect and save the current selection in the monthNameDropdown Optionmenu so I change the amount of days in the monthDayDropdown Optionmenu, bu it's giving me a strange error that I do not understand. Can someone please help me resolve this?

from tkinter import *
import datetime as dt
from tkinter import font
import tkinter.ttk as ttk
from turtle import bgcolor

date = dt.datetime.now()

window = Tk()
title = Label(window, text='Date:', font=("Arial", 22, font.BOLD)).pack()

def main():

    style = ttk.Style(window)
    window.tk.call('source', '/Users/Amarn/OneDrive - Da Vinci College/Da Vinci College/software_developen/Assignments/jaar_1/periode_1/fase_1/08_wat_zie_ik_nou_GUI-beuren/leerpad_03/gui-formulieren/themes/sun-valley.tcl')
    style.theme_use('sun-valley-light')
    
    
    
    centerFrame = Frame(window)
    centerFrame.pack(pady=30)
    
    monthDay = f'{date:%d}'
    monthName = f'{date:%b}'
    year = f'{date:%Y}'
    
    window.title('Days Calculator')
    window.geometry('400x200')
    
    
    monthDayStringVar = StringVar(window)
    monthDayStringVar.set(monthDay)
    
    monthNameStringVar = StringVar(window)
    monthNameStringVar.set(monthName)
    
    yearStringVar = StringVar(window)
    yearStringVar.set(year)
    
    
    print(monthDay)
    print(monthName)
    print(year)
    
    monthList = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    monthDaysList = []
    score = 1
        
    
    def monthDayAmount(monthName):
        if monthName == 'Jan' or monthName == 'Mar' or monthName == 'May' or monthName == 'Jul' or monthName == 'Aug' or monthName == 'Oct' or monthName == 'Dec':
            monthDayAmountVar = 32
            print('31 days')
        if monthName == 'Feb':
            monthDayAmountVar = 29
            print('28 days')
        if monthName == 'Apr' or monthName == 'Jun' or monthName == 'Sep' or monthName == 'Nov':
            monthDayAmountVar = 31
            print('30 days')
        return monthDayAmountVar                           
        
    monthDayAmountVar = monthDayAmount(monthName)
    
    for a in range(monthDayAmountVar):
        score = f'{a}'
    
        monthDaysList.append(score)
    print(monthDaysList)        
    
    monthDayDropdown = ttk.OptionMenu(centerFrame, monthDayStringVar)
    monthDayDropdown.config(width=6)
    monthDayDropdown.pack(side=LEFT)
    
    
    
    line1 = Label(centerFrame, text='-', font=("Arial", 15, font.BOLD))
    line1.pack(side=LEFT)
    
    monthNameDropdown = ttk.OptionMenu(centerFrame, monthNameStringVar, *monthList)
    monthNameDropdown.config(width=6)
    monthNameDropdown.pack(side=LEFT)
    
    def displaySelected():
        print('Je hebt dit gekozen').format(monthNameStringVar.get())
    
    monthNameStringVar.trace('monthNameDropdown', displaySelected)
    
    line2 = Label(centerFrame, text='-', font=("Arial", 15, font.BOLD))
    line2.pack(side=LEFT)
    
    yearDropdown = ttk.OptionMenu(centerFrame, yearStringVar, '2023')
    yearDropdown.config(width=6)
    yearDropdown.pack(side=LEFT)
    
    def buttonClick():
        goButton = ttk.Button(window, text="GO", style="Accent.TButton")
        goButton.config(width=10)
        goButton.pack(side=TOP)
    buttonClick()

main()

window.mainloop()

This is the error it's outputting:

Traceback (most recent call last): File "c:\Users\Amarn\OneDrive - Da Vinci College\Da Vinci College\software_developen\Assignments\jaar_1\periode_1\fase_1\08_wat_zie_ik_nou_GUI-beuren\leerpad_03\gui-formulieren\days-calculator-100%.py", line 102, in main() File "c:\Users\Amarn\OneDrive - Da Vinci College\Da Vinci College\software_developen\Assignments\jaar_1\periode_1\fase_1\08_wat_zie_ik_nou_GUI-beuren\leerpad_03\gui-formulieren\days-calculator-100%.py", line 87, in main monthNameStringVar.trace('monthNameDropdown', displaySelected) File "C:\Users\Amarn\AppData\Local\Programs\Python\Python39\lib\tkinter_init_.py", line 456, in trace_variable self._tk.call("trace", "variable", self._name, mode, cbname) _tkinter.TclError: bad operations "monthNameDropdown": should be one or more of rwua

python

tkinter

optionmenu

ttkwidgets

tkinter.optionmenu

0 Answers

Your Answer

Accepted video resources