3

I need to create a choice box
choice box
where i can click on arrow and it give me list of choices.
choice box pic 2
And if i click on one of them it will change it in that first rectangle.
Its possible to do something like this? Thank you for any idea.

Community
  • 1
  • 1
Nefritox
  • 147
  • 1
  • 3
  • 10

3 Answers3

7

You can also try an OptionMenu:

from Tkinter import *

root = Tk()

choices = ['GB', 'MB', 'KB']
variable = StringVar(root)
variable.set('GB')

w = OptionMenu(root, variable, *choices)
w.pack(); root.mainloop()

OptionMenu example

Or you can try using a Combobox:

from ttk import *
from Tkinter import *

root = Tk()

choices = ['GB', 'MB', 'KB']
variable = StringVar(root)
variable.set('GB')

w = Combobox(root, values = choices)
w.pack(); root.mainloop()

Combobox example

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
5

Tkinter has two widgets that do what you want. One is OptionMenu and the other is ttk.Combobox.

import Tkinter as tk
import ttk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        choiceVar = tk.StringVar()
        choices = ("choice 1", "choice 2", "choice 3", "choice 4")
        choiceVar.set(choices[0])

        om = tk.OptionMenu(self, choiceVar, *choices)
        cb = ttk.Combobox(self, textvariable=choiceVar, values=choices)

        om.pack()
        cb.pack()

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

ComboBox and OptionMenu

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
-1

There is a class that someone made for a drop down list view. Using this class, you can try:

from Tkinter import *

# insert class here

root = Tk()

view = ChoiceBox(root, ['MB', 'KB', 'GB', 'TB'])
view.place_configure(x = 0, y = 0)

root.mainloop()
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
  • because a) there are built-in widgets that do what the OP wants, and b) it forces the reader to go to another site, and then scroll through a bunch of responses to find the code. – Bryan Oakley Mar 06 '15 at 19:50
  • You can also use an option menu which you can see in another answer below. You had no trouble with the class right? – Malik Brahimi Mar 06 '15 at 19:50
  • Bryan Oakley, I made another answer including an option menu. And for the record, scrolling is not an inconvenience. This profession is one in which you have to exert a slight amount of effort. And you're link answer isn't much better. – Malik Brahimi Mar 06 '15 at 19:52
  • 2
    @MalikBrahimi: it's not so much the inconvenience of scrolling. Answers that link to code on other websites are against the spirit of stackoverflow. That other site could go down or get reorganized and then your answer becomes completely useless. Plus, all of the discussion you linked to is misleading because it is so old. A beginner who reads that may get the impression there are no built-in widgets that do what they want, when in fact there is not just one, but two. – Bryan Oakley Mar 06 '15 at 20:06
  • @MalikBrahimi Merge this answer with the other. No need to have 2 answers from the same person, it creates more confusion, in my opinion. – nbro Mar 07 '15 at 12:31