Python Forum
How to create a menu button?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a menu button?
#1
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.
Reply
#2
Menu is a poor choice. Combobutton or optionmenu are a better fit.
Reply
#3
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.
Reply
#4
You used an OptionMenu in the program you wish to reproduce. I like the Combobutton better, but OptionMenu is ok.
Reply
#5
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.
Reply
#6
For some reason I am having difficulty reading your mind. Maybe you should post a question.
Pedroski55 likes this post
Reply
#7
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.
Reply
#8
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()
Reply
#9
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.
Reply
#10
You want to do this, but in a GUI.
import calendar

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I create menu in Python? khagan07 1 1,136 Oct-07-2023, 12:44 PM
Last Post: deanhystad
  Create a dynamic Menu from a editable Dictionary. KiNeMs 1 2,937 Jan-28-2020, 04:27 AM
Last Post: Larz60+
  Create menu with selectable items SaladFingers 4 4,156 Sep-28-2019, 11:23 AM
Last Post: SaladFingers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020