Python Forum
How to create a menu button? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to create a menu button? (/thread-36529.html)

Pages: 1 2 3 4


How to create a menu button? - Frankduc - Mar-01-2022

Hi all,

I came up with this finding about creating a menu button to populate with months of the year for a calendar.

def affCal(self, mois, annee):

    mb = Boutonmenu(root, text = 'Mois')
    mb.menu = Menu(mb)
    mb['menu'] = mb.menu

//thought about creatint a for loop of list of months ['january', ...] pass it in the label than pass somehow resultat.insert(tk.INSERT, calendar.month(annee, mois)) into the lamba and than return resultat.insert! 


    mb.menu.add_command(label = 'janvier', command = lambda: ? )

    return resultat.insert(tk.INSERT, calendar.month(annee, mois))
I am kind of in a deadlock.

I want to pass the year (anne) and the month (mois) into calendar.month(annee, mois) method.
Any suggestion is welcome.


RE: How to create a menu button? - deanhystad - Mar-01-2022

Menu is a poor choice. Combobutton or optionmenu are a better fit.


RE: How to create a menu button? - Frankduc - Mar-01-2022

Most of those solutions are to complicated to my taste. I just want to reproduce what i did for this code.

from tkinter import *


def conversion():
    if choix.get() == 'Celcius à Fahrenheit':
        celcius = float(entree.get())
        fahrenheit = (celcius * 1.8) + 32
        resultat.configure(text=str(fahrenheit))

    else:
        fahrenheit = float(entree.get())
        celcius = (fahrenheit - 32) / 1.8
        resultat.configure(text=str(celcius))


def changement(event):
    if choix.get() == 'Celcius à Fahrenheit':
        txt1.configure(text='Température en Celcius')
        txt2.configure(text='Température en Fahrenheit')
    else:
        txt2.configure(text='Température en Celcius')
        txt1.configure(text='Température en Fahrenheit')


fen1 = Tk()
fen1.title('Conversion')

txt1 = Label(fen1, text='Température en Fahrenheit')
txt1.grid(row=0, column=0, sticky=E)

entree = Entry(fen1)
entree.grid(row=0, column=1)

txt2 = Label(fen1, text='Température en Celcius')
txt2.grid(row=1, column=0, sticky=E)

resultat = Label(fen1, text='')
resultat.grid(row=1, column=1)

bouton = Button(fen1, text='Conversion', command=conversion)
bouton.grid(row=2, column=0)

choix = StringVar(fen1)
choix.set('Celcius à Fahrenheit')
liste = OptionMenu(fen1, choix, 'Celcius à Fahrenheit', 'Fahrenheit à Celcius', command=changement)

liste.grid(row=2, column=1)

fen1.mainloop()
Find a way to include a list or enumerate in def changement(event):
I am wasting to much time on this.


RE: How to create a menu button? - deanhystad - Mar-01-2022

You used an OptionMenu in the program you wish to reproduce. I like the Combobutton better, but OptionMenu is ok.


RE: How to create a menu button? - Frankduc - Mar-01-2022

OptionMenu is what they showed in the book and probably in class. Better stick with it. I lost a lot of pts at the exam at the last question for a stupity like this. Teacher strip you of a lot of pts on little details, got 60 for last question and it drop me to 80.
I am stuck i dont know what to do.


RE: How to create a menu button? - deanhystad - Mar-01-2022

For some reason I am having difficulty reading your mind. Maybe you should post a question.


RE: How to create a menu button? - Frankduc - Mar-01-2022

I try to formulate the right question, for the right issue.

Do i need the def changement(event): ?

After all maybe i should just create a list of months:
list_months = ['january', 'febrary', ....]
for m in list_months:
if choix.get() == m:
get the index of the month and pass it into the: return resultat.insert(tk.INSERT, calendar.month(annee, month))

For now thats the only plan i came with to stay in line with the book.


RE: How to create a menu button? - deanhystad - Mar-01-2022

Why would you want the index?
iimport tkinter as tk

months = ("Way before March", "Before March", "March", "After March")

def month_changed(label, value):
    index = months.index(value)
    label["text"] = f"Beware the ides of {value} {index}"

root = tk.Tk()
choix = tk.StringVar(root, value=months[0])

tk.Label(root, textvariable=choix, width=30).pack()

label = tk.Label(root, width=30)
label.pack()

tk.OptionMenu(root, choix, *months).pack(fill=tk.X)

choix.trace_add("write", lambda a, b, c: month_changed(label, choix.get()))

root.mainloop()



RE: How to create a menu button? - Frankduc - Mar-01-2022

This is returning the calendar by inputing year and month.

resultat.insert(tk.INSERT, calendar.month(year, month))
I have to pass the value of the month. Cannot input january as a string. The method is taking a number 0 = january, 1 = february and on.

calendar.month(2022, 1) 1 for february Thats why i am saying i could create a list of the months. Than extract the index and pass index in the calendar method.
I just have to figure out how to create the option menu and connect all this.

You always have interesting solutions but a bit sophiesticated for the situation.


RE: How to create a menu button? - deanhystad - Mar-01-2022

You want to do this, but in a GUI.
import calendar

print(calendar.month(2022, 3))
Correct?