Window with tk for receiving text

I am creating a small training application.

I already have all the code, all I am missing is a way to open a window with TK showing the text box, image and button.

All he has to do is return the text inserted in the text box after clicking the button and closing the windows.

So how do I do this?

I looked at the code, but nothing worked, I am almost ashamed that it is so simple.

thank

+3
source share
2 answers

An easy way to create graphical interfaces is to use Tkinter. Here is an example that displays windows with text and a button:

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

You can see the Tkinter documentation, the tag widget also supports the inclusion of images.

Hi

+2
source

enter image description here

, inputBox myText. . , , . , , , image = tk.PhotoImage(data=b64_data). , b64_data = .... . ( MAC 10.6 Python 3.2). 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: http://effbot.org/tkinterbook/entry.htm

, :

+2

All Articles