Question

I wonder if it is possible to make the text in a text box converted to a variable(/string). I am using Python 3.3.5 and I have seen a lots of Entrybox to variable but it did not work on a textbox.
Here is the code I am working with:

from tkinter import *

    main = Tk()
    sr=Scrollbar(main)
    a="Hello!"

    txtBox = Text(main, width=20, height=5)
    sr.pack(side=RIGHT)
    txtBox.pack(side=LEFT)
    sr.config(command=txtBox.yview)
    txtBox.config(yscrollcommand=sr.set)
    txtBox.insert(END, a)

    def callback():
        #here is the text converted to a variabel#

    def closewindow():
        exit()

    def save():
        key = #variabel#
        f = open("key.txt", "w")
        f.write(key)
        f.close()
        print(txtBox.get()+(" /has bin saved."))

    button_dsp = Button(main, text="display text", width=10, command=callback)
    button_dsp.pack()

    button_save = Button(main, text="Save", width=10, command=save)
    button_save.pack()

    button_exit = Button(main, text="Exit", width=10, command=closewindow)
    button_exit.pack()
    mainloop()
Was it helpful?

Solution

You show the solution in your save function, but for your benefit in your current structure it will look a bit like this

def callback():
    return txtBox.get()

def save():
    key = callback()

simplified, get rid of the callback function and just amend your save function to:

def save():
    key = txtBox.get()

This is of course assuming you don't want the entry to become the name of the variable. That would be silliness defined.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top