0

I'm missing some basic comprehension of how the tkinter dialogs are supposed to work with the rest of the code in a Python program. Perhaps my approach is not the best, but what I am trying to do is the following:

Run some code to select some files (works)
Parse the files (works)
Based on what is in the files, open a simple combo-box with options to choose from (doesn't work)
Based on the choice the user makes, perform different actions on the files (doesn't get this far)

tkinter has a very easy-to-use function to select a directory graphically:

root.dirname =  askdirectory(initialdir = "/home/jon/Python/",title = "Choose directory")

I can use that any time during my program, select a directory, store it in root.dirname, and use root.dirname later in my program as necessary. I would like to do something very similar to that with a drop-down menu. I'm picturing something like the following:

selectedOption = dropdownbox(('Option1', 'Option2', 'Option3'))

Then a simple dialog box would open, presenting the user with a dropdown box with the three options. After the user chooses the option they want (and presses OK or something to that affect), the window closes and selectedOption contains the option that was selected. Then my code can continue and use selectedOption to determine what course to take.

The main gap in my understanding is how to open a tkinter dialog at an arbitrary location in my code and use the "result" of the dialog later in my code. All of the code I've been finding for tkinter comboboxes, etc, seem to be more along the lines of building a standalone application rather than a short dialog to make a decision and return control to the calling function. But I think I'm just missing something. Any help is appreciated.

EDIT: Added code based on Uriel's input

from tkinter import simpledialog
from tkinter import *
def returnResultFromSimpleDialog():

        root = Tk()
        root.result = simpledialog.askstring('Input', 'Enter a string') # title and description
        root.withdraw()
        return root.result

if __name__ == '__main__':
        print(returnResultFromSimpleDialog())

This is very close to what I am wanting to do, except I want a window with a dropdown menu instead of a text box.

Rusty Lemur
  • 1,697
  • 1
  • 21
  • 54

1 Answers1

1

That way:

import tkinter.simpledialog
result = tkinter.simpledialog.askstring('Input', 'Enter a string') # title and description

Note - to use tkinter's dialogs (the only ones available) you must activate it from within a tkinter application.

A hack would be to call this line twice (first one crashes - handle it - and opens a window), then the second call has a parent window:

>>> import tkinter.simpledialog
>>> result = tkinter.simpledialog.askstring('Input', 'Enter a string') # title and description
Traceback (most recent call last):
  ...
AttributeError: 'NoneType' object has no attribute 'winfo_viewable'
>>> result = tkinter.simpledialog.askstring('Input', 'Enter a string') # title and description
>>> result
'hello'
Uriel
  • 15,579
  • 6
  • 25
  • 46
  • Thanks Uriel. Is there a simplecombobox type construct where I can pass options I want available and receive the selection? – Rusty Lemur Oct 26 '16 at 00:13
  • 1
    you can design your own popup but that would be already a gui app - see here http://stackoverflow.com/questions/10057672/correct-way-to-implement-a-custom-popup-tkinter-dialog-box – Uriel Oct 26 '16 at 00:25