私は小さな教育アプリを構築しています。
私はすでにすべてのコードを完了していますが、欠けているのは、テキストボックス、画像、およびボタンを表示する TK でウィンドウを開く方法だけです。
ボタンをクリックしてウィンドウを閉じた後、テキストボックスに挿入されたテキストを返します。
それで、どうすればいいですか?
私はコードを見てきましたが、何も機能しませんでした。これが非常に基本的であることを恥ずかしく思いました。
ありがとう
GUI を作成する簡単な方法は、Tkinter を使用することです。テキストとボタンを含むウィンドウを表示する例があります:
from Tkinter import*
class GUI:
def __init__(self,v):
self.entry = Entry(v)
self.entry.pack()
self.button=Button(v, text="Press the button",command=self.pressButton)
self.button.pack()
self.t = StringVar()
self.t.set("You wrote: ")
self.label=Label(v, textvariable=self.t)
self.label.pack()
self.n = 0
def pressButton(self):
text = self.entry.get()
self.t.set("You wrote: "+text)
w=Tk()
gui=GUI(w)
w.mainloop()
Tkinter のドキュメントを参照してください。ラベル ウィジェットは画像の追加もサポートしています。
よろしく
inputBox
これはtoから入力を取得する単純なコードですmyText
。それはあなたを正しい方向へと導くはずです。他に確認または実行する必要があることに応じて、さらに機能を追加できます。行の順序をいじる必要があるかもしれないことに注意してくださいimage = tk.PhotoImage(data=b64_data)
。の直後に置くとb64_data = ...
。エラーが発生します。(Python 3.2 で MAC 10.6 を実行しています)。現時点では、画像は GIF でのみ機能します。さらに詳しく知りたい場合は、下部のリファレンスを参照してください。
import tkinter as tk
import urllib.request
import base64
# Download the image using urllib
URL = "http://www.contentmanagement365.com/Content/Exhibition6/Files/369a0147-0853-4bb0-85ff-c1beda37c3db/apple_logo_50x50.gif"
u = urllib.request.urlopen(URL)
raw_data = u.read()
u.close()
b64_data = base64.encodestring(raw_data)
# The string you want to returned is somewhere outside
myText = 'empty'
def getText():
global myText
# You can perform check on some condition if you want to
# If it is okay, then store the value, and exist
myText = inputBox.get()
print('User Entered:', myText)
root.destroy()
root = tk.Tk()
# Just a simple title
simpleTitle = tk.Label(root)
simpleTitle['text'] = 'Please enter your input here'
simpleTitle.pack()
# The image (but in the label widget)
image = tk.PhotoImage(data=b64_data)
imageLabel = tk.Label(image=image)
imageLabel.pack()
# The entry box widget
inputBox = tk.Entry(root)
inputBox.pack()
# The button widget
button = tk.Button(root, text='Submit', command=getText)
button.pack()
tk.mainloop()
Tkinter Entry ウィジェットについて詳しく知りたい場合は、こちらを参照してください: http://effbot.org/tkinterbook/entry.htm
画像の取得方法の参考:Stackoverflow Question