2016-10-25 13 views
0

Pythonプログラムの残りのコードとtkinterダイアログがどのように機能するのか、基本的な理解がありません。おそらく、私のアプローチが最善ではありませんが、私は何をしようとしていることは以下の通りです:tkinterダイアログボックスを開き、後でプログラム内の結果を使用する方法

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

I:

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にはグラフィカルにディレクトリを選択するために非常に簡単に使用できる機能を持っています私のプログラム中にいつでもそれを使うことができます。ディレクトリを選択し、それをroot.dirnameに格納し、後で必要に応じてroot.dirnameを自分のプログラムで使用します。私はドロップダウンメニューでそれに非常に似た何かをしたいと思います。私は、次のようなものを描いています:

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

が次に簡単なダイアログボックスが開くだろう、3つのオプションを持つドロップダウンボックスをユーザに提示します。ユーザーが望むオプションを選択した後(OKを押すかその影響を受ける)、ウィンドウが閉じ、selectedOptionには選択されたオプションが含まれます。それから、私のコードを続けて、selectedOptionを使って、どのコースを取るべきかを決定することができます。

私の理解の主な違いは、コード内の任意の場所でtkinterダイアログを開き、後でコードの「結果」を使用する方法です。私がtkinterコンボボックスなどで見つけたすべてのコードは、決定を下して呼び出し関数に制御を戻すための短いダイアログではなく、スタンドアロンのアプリケーションを構築する行に沿っているようです。しかし、私は何かが欠落していると思う。どんな助けもありがとうございます。

EDIT:ウリエルの入力に基づいてコードを追加しました

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()) 

これは私が代わりにテキストボックスのドロップダウンメニューでウィンドウをしたい除いて、やりたいと思っていますものに非常に近いです。

答えて

1

その方法:

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

注 - tkinterアプリケーション内から、それを有効にする必要がありtkinterのダイアログ(利用可能な唯一のもの)を使用します。

>>> 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' 
+0

おかげウリエル:

ハックは二回この行を呼び出すことであろう(最初のものがクラッシュ - - それを処理して、ウィンドウを開きます)、その後、2回目の呼び出しは、親ウィンドウを持っています。利用可能なオプションを渡して選択を受け取ることができるsimplecombobox型のコンストラクタがありますか? –

+1

あなた自身のポップアップをデザインすることはできますが、すでにGUIアプリケーションになっているでしょう - http://stackoverflow.com/questions/10057672/correct-way-to-implement-a-custom-popup-tkinter-dialog-box – Uriel

関連する問題