a=QuestionBox("質問箱","temp.txt")
とすると、質問用のメッセージボックスが出て、ボタンが押されたら書かれたテキストを、ファイルに書き込んだ後、そのテキストを戻り値とするコードを作っています。
しかしながら、下記のコードのFlagWright():の処理では、ファイルには正しく書き込まれるのですが、戻り値がnoneとなってしまいます。
どうしたら、正しく戻り値がaに入力されるのでしょうか?
追記
root.mainloop()の後にreturn numと書くと、その値は戻り値としてaに代入される。
def QuestionBoxの中の内部関数としてdef FlagWrightを書いたが、ここでreturn numとやってもdef QuestionBoxの中にしか戻らないみたいです。
関数の中の内部関数の戻り値を取り出したいと、タイトルを変更します。
Python
1import tkinter 2import os 3 4def QuestionBox (mes, filename="none", x=50, y=50, width=900, height=150, title="Question", 5 workdir="C:\temp\"): 6 # Tkクラス生成 7 root = tkinter.Tk() 8 root.geometry('{}x{}+{}+{}'.format(width, height, x, y)) 9 root.title(title) 10 root.attributes("-topmost", True) 11 root.option_add('*font', (u'MS ゴシック', 20)) 12 13 # ラベル 14 lbl = tkinter.Label(text=mes) 15 lbl.place(x=30, y=10) 16 17 # テキストボックス 18 # txt = tkinter.Entry(width=7, font="u'MS ゴシック', 20, 'bold'", title="Question") 19 txt = tkinter.Entry(width=7) 20 txt.option_add('*font', (u'MS ゴシック', 20, 'bold')) 21 22 23 txt.place(x=1150, y=15) 24 txt.pack(padx=5, pady=5, anchor=tkinter.SE) 25 # txt.insert(tkinter.END, "3") 26 27 def FlagWright(): 28 num = txt.get() 29 if filename != "none": 30 pathstring = os.path.join(workdir,filename) 31 with open(pathstring, 'w', encoding='utf-8') as file: 32 file.write(num) 33 root.quit() 34 return num 35 36 # button1 = tkinter.Button(root, text='決定', command=lambda: make_cmd(root), 37 button1 = tkinter.Button(root, text='決定', width=10, height=2, command=lambda :FlagWright()) 38 button1.pack(side=tkinter.BOTTOM, padx=5, pady=5, anchor=tkinter.SE) 39 40 root.mainloop() 41
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/16 09:06
2018/11/16 09:46