我正在构建一个小型教育应用程序。使用tk获取文本的窗口
我已经完成了所有的代码,我所缺少的一切都是让窗口打开并显示文本框,图像和按钮的TK。
它应该做的所有事情,它在单击按钮并关闭窗口后返回插入到文本框中的文本。
那么,我该怎么做?
我一直在看代码,但没有任何工作,我几乎感到羞耻,因为这是如此基本。
谢谢
我正在构建一个小型教育应用程序。使用tk获取文本的窗口
我已经完成了所有的代码,我所缺少的一切都是让窗口打开并显示文本框,图像和按钮的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的文档,标签控件还支持包括图片。
问候
如果你想返回文本,那么在pressButton方法中,你可以退出窗口然后返回文本。 – Carlos
用你的回复来构造我需要的东西!谢谢! –
这是一个简单的代码,您可以通过inputBox
输入到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控件:Stackoverflow Question
这工作完美,谢谢! –
的应用程序是一个命令行之一:如何让图像http://effbot.org/tkinterbook/entry.htm
参考? – jadkik94
您是否有任何代码示例,您已经使用Tk尝试过,因此社区可以看到可能出错的内容? – jdi
我很高兴地说我没有搬到PySide,并学会了如何使用它。这一切现在看起来更容易。 –