RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

tkinter Drop Down Menu Program (convert to Python 3)

Sun Feb 10, 2019 7:47 pm

I would like to incorporate a drop down menu in my Python 3 program.
I have found the following program on the internet but it is a Python 2 program.
I realise that Tkinter needs changing to tkinter but it then fails on the next line, with an error regarding no module called ttk

I would appreciate help to find a Python 3 alternative to ttk.

Code: Select all

# This is a Python 2 program

from Tkinter import *
import Tkinter as ttk
from ttk import *
 
root = Tk()
root.title("Tk Drop Down Menu")
 
# Add a grid
mainframe = Frame(root)
mainframe.grid(column=0,row=0, sticky=(N,W,E,S) )
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
mainframe.pack(pady = 100, padx = 100)
 
# Create a Tkinter variable
tkvar = StringVar(root)
 
# Dictionary with options
choices = [ '1,000','2,000','3,000','4,000','5,000','10,000','15,000']
tkvar.set('10,000') # set the default option
 
popupMenu = OptionMenu(mainframe, tkvar, choices[1], *choices)
Label(mainframe, text="Choose an Option").grid(row = 1, column = 1)
popupMenu.grid(row = 2, column =1)

 
# on change dropdown value
def change_dropdown(*args):
    print( tkvar.get() )
 
# link function to change dropdown
tkvar.trace('w', change_dropdown)
 
root.mainloop()

scotty101
Posts: 3670
Joined: Fri Jun 08, 2012 6:03 pm

Re: tkinter Drop Down Menu Program (convert to Python 3)

Sun Feb 10, 2019 9:48 pm

The official python docs show the following

Code: Select all

from tkinter import ttk
https://docs.python.org/3/library/tkinter.ttk.html

However your code doesn't seem to use any ttk widgets. Have you tried commenting the import ttk line out?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
paddyg
Posts: 2336
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: tkinter Drop Down Menu Program (convert to Python 3)

Sun Feb 10, 2019 10:10 pm

I think the idea of "import ... *" is a grave mistake anyway. https://www.python.org/dev/peps/pep-0008/#imports
better to make it clear where things come from rather than making them 'just' appear..

Code: Select all

import tkinter as tk
root = tk.Tk()
root.title("Tk Drop Down Menu")
mainframe = tk.Frame(root)
mainframe.grid(column=0,row=0, sticky=(tk.N, tk.W, tk.E, tk.S))
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

RDS
Posts: 712
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: tkinter Drop Down Menu Program (convert to Python 3)

Sun Feb 10, 2019 10:57 pm

@scotty
Thank you. I thought I had tried to comment those lines out but I now think I must have just been trying to remove one at a time. I have now commented both of them out and it runs perfectly (after also changing Tkinter to tkinter).

@paddyg
I had also read about only specifying the elements that are required rather than using import* but my existing program uses 'from tkinter import*', so I didn't want to upset the running of my main program.

rzane0110
Posts: 3
Joined: Wed Feb 13, 2019 8:18 am

Re: tkinter Drop Down Menu Program (convert to Python 3)

Wed Feb 13, 2019 9:27 am

The widget is called OptionMenu and the parameters you need are: frame, tk variable, free steam games with free steam accounts and a dictionary with choices.
It starts by creating a Tk object and pass it to a tkinter frame created with Frame()

root = Tk()
root.title("Tk dropdown example")
mainframe = Frame(root)
A grid is added to the frame which will hold the combo-box.

mainframe.grid(column=0,row=0, sticky=(N,W,E,S) )
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
mainframe.pack(pady = 100, padx = 100)
The popup menu contains a list of options which is defined in the variable choices.
A Tkinter variable is created with the line:

tkvar = StringVar(root)
The default value of the variable is set with the .set() method.
We create the Tkinter combobox with:

popupMenu = OptionMenu(mainframe, tkvar, *choices)
And link the callback method change_dropdown to this combobox.
Last edited by rzane0110 on Wed Apr 03, 2019 1:40 pm, edited 1 time in total.

User avatar
RogerW
Posts: 285
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: tkinter Drop Down Menu Program (convert to Python 3)

Wed Feb 13, 2019 10:16 am

This is a "template" python 3 program that uses includes a menu. It might help.

Code: Select all

import tkinter as tk
import tkinter.messagebox as msg

class Mainframe(tk.Frame):
    # Mainframe contains the widgets
    # More advanced programs may have multiple frames
    # or possibly a grid of subframes
    
    def __init__(self,master,*args,**kwargs):
        # *args packs positional arguments into tuple args
        #  **kwargs packs keyword arguments into dict kwargs
        
        # initialise base class
        tk.Frame.__init__(self,master,*args,**kwargs)
        # in this case the * an ** operators unpack the parameters
        
        # you will put your widgets here
        tk.Button(self,text = 'Quit',width = 25,command = master.destroy).pack()
        

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
               
        # set the title bar text
        self.title('My App')
        # Make sure app window is big enough to show title 
        self.geometry('300x100')
      
        # create a menu bar
        # if you do not need menus discard the following code
        mainMenu = tk.Menu(self)
        self.config(menu=mainMenu)
        
        # create a file menu with an exit entry
        # you may need to add more entries
        fileMenu = tk.Menu(mainMenu)
        fileMenu.add_command(label='Exit',command=self.destroy)
        mainMenu.add_cascade(label='File',menu=fileMenu)
        
        # any main menu should have a help entry
        helpMenu = tk.Menu(mainMenu)
        helpMenu.add_command(label = 'about',command = self.showAbout)
        mainMenu.add_cascade(label = 'Help',menu = helpMenu)
        
        # create and pack a Mainframe window
        Mainframe(self).pack()
        
        # now start
        self.mainloop()
        
    def showAbout(self):
        # show the about box
        msg.showinfo('About', 'This is a template Python/tkinter app')
            
# create an App object
# it will run itself
App()

Return to “Python”