Related
How to get Tkinter input from the Text widget?
EDIT
I asked this question to help others with the same problem - that is the reason why there is no example code. This issue had been troubling me for hours and I used this question to teach others. Please do not rate it as if it was a real question - the answer is the thing that matters.
To get Tkinter input from the text box, you must add a few more attributes to the normal .get() function. If we have a text box myText_Box, then this is the method for retrieving its input.
def retrieve_input():
input = self.myText_Box.get("1.0",END)
The first part, "1.0" means that the input should be read from line one, character zero (ie: the very first character). END is an imported constant which is set to the string "end". The END part means to read until the end of the text box is reached. The only issue with this is that it actually adds a newline to our input. So, in order to fix it we should change END to end-1c(Thanks Bryan Oakley) The -1c deletes 1 character, while -2c would mean delete two characters, and so on.
def retrieve_input():
input = self.myText_Box.get("1.0",'end-1c')
Here is how I did it with python 3.5.2:
from tkinter import *
root=Tk()
def retrieve_input():
inputValue=textBox.get("1.0","end-1c")
print(inputValue)
textBox=Text(root, height=2, width=10)
textBox.pack()
buttonCommit=Button(root, height=1, width=10, text="Commit",
command=lambda: retrieve_input())
#command=lambda: retrieve_input() >>> just means do this when i press the button
buttonCommit.pack()
mainloop()
with that, when i typed "blah blah" in the text widget and pressed the button, whatever i typed got printed out. So i think that is the answer for storing user input from Text widget to variable.
To get Tkinter input from the text box in python 3 the complete student level program used by me is as under:
#Imports all (*) classes,
#atributes, and methods of tkinter into the
#current workspace
from tkinter import *
#***********************************
#Creates an instance of the class tkinter.Tk.
#This creates what is called the "root" window. By conventon,
#the root window in Tkinter is usually called "root",
#but you are free to call it by any other name.
root = Tk()
root.title('how to get text from textbox')
#**********************************
mystring = StringVar()
####define the function that the signup button will do
def getvalue():
## print(mystring.get())
#*************************************
Label(root, text="Text to get").grid(row=0, sticky=W) #label
Entry(root, textvariable = mystring).grid(row=0, column=1, sticky=E) #entry textbox
WSignUp = Button(root, text="print text", command=getvalue).grid(row=3, column=0, sticky=W) #button
############################################
# executes the mainloop (that is, the event loop) method of the root
# object. The mainloop method is what keeps the root window visible.
# If you remove the line, the window created will disappear
# immediately as the script stops running. This will happen so fast
# that you will not even see the window appearing on your screen.
# Keeping the mainloop running also lets you keep the
# program running until you press the close buton
root.mainloop()
In order to obtain the string in a Text widget one can simply use get method defined for Text which accepts 1 to 2 arguments as start and end positions of characters, text_widget_object.get(start, end=None). If only start is passed and end isn't passed it returns only the single character positioned at start, if end is passed as well, it returns all characters in between positions start and end as string.
There are also special strings, that are variables to the underlying Tk. One of them would be "end" or tk.END which represents the variable position of the very last char in the Text widget. An example would be to returning all text in the widget, with text_widget_object.get('1.0', 'end') or text_widget_object.get('1.0', 'end-1c') if you don't want the last newline character.
Demo
See below demonstration that selects the characters in between the given positions with sliders:
try:
import tkinter as tk
except:
import Tkinter as tk
class Demo(tk.LabelFrame):
"""
A LabeFrame that in order to demonstrate the string returned by the
get method of Text widget, selects the characters in between the
given arguments that are set with Scales.
"""
def __init__(self, master, *args, **kwargs):
tk.LabelFrame.__init__(self, master, *args, **kwargs)
self.start_arg = ''
self.end_arg = None
self.position_frames = dict()
self._create_widgets()
self._layout()
self.update()
def _create_widgets(self):
self._is_two_args = tk.Checkbutton(self,
text="Use 2 positional arguments...")
self.position_frames['start'] = PositionFrame(self,
text="start='{}.{}'.format(line, column)")
self.position_frames['end'] = PositionFrame( self,
text="end='{}.{}'.format(line, column)")
self.text = TextWithStats(self, wrap='none')
self._widget_configs()
def _widget_configs(self):
self.text.update_callback = self.update
self._is_two_args.var = tk.BooleanVar(self, value=False)
self._is_two_args.config(variable=self._is_two_args.var,
onvalue=True, offvalue=False)
self._is_two_args['command'] = self._is_two_args_handle
for _key in self.position_frames:
self.position_frames[_key].line.slider['command'] = self.update
self.position_frames[_key].column.slider['command'] = self.update
def _layout(self):
self._is_two_args.grid(sticky='nsw', row=0, column=1)
self.position_frames['start'].grid(sticky='nsew', row=1, column=0)
#self.position_frames['end'].grid(sticky='nsew', row=1, column=1)
self.text.grid(sticky='nsew', row=2, column=0,
rowspan=2, columnspan=2)
_grid_size = self.grid_size()
for _col in range(_grid_size[0]):
self.grid_columnconfigure(_col, weight=1)
for _row in range(_grid_size[1] - 1):
self.grid_rowconfigure(_row + 1, weight=1)
def _is_two_args_handle(self):
self.update_arguments()
if self._is_two_args.var.get():
self.position_frames['end'].grid(sticky='nsew', row=1, column=1)
else:
self.position_frames['end'].grid_remove()
def update(self, event=None):
"""
Updates slider limits, argument values, labels representing the
get method call.
"""
self.update_sliders()
self.update_arguments()
def update_sliders(self):
"""
Updates slider limits based on what's written in the text and
which line is selected.
"""
self._update_line_sliders()
self._update_column_sliders()
def _update_line_sliders(self):
if self.text.lines_length:
for _key in self.position_frames:
self.position_frames[_key].line.slider['state'] = 'normal'
self.position_frames[_key].line.slider['from_'] = 1
_no_of_lines = self.text.line_count
self.position_frames[_key].line.slider['to'] = _no_of_lines
else:
for _key in self.position_frames:
self.position_frames[_key].line.slider['state'] = 'disabled'
def _update_column_sliders(self):
if self.text.lines_length:
for _key in self.position_frames:
self.position_frames[_key].column.slider['state'] = 'normal'
self.position_frames[_key].column.slider['from_'] = 0
_line_no = int(self.position_frames[_key].line.slider.get())-1
_max_line_len = self.text.lines_length[_line_no]
self.position_frames[_key].column.slider['to'] = _max_line_len
else:
for _key in self.position_frames:
self.position_frames[_key].column.slider['state'] = 'disabled'
def update_arguments(self):
"""
Updates the values representing the arguments passed to the get
method, based on whether or not the 2nd positional argument is
active and the slider positions.
"""
_start_line_no = self.position_frames['start'].line.slider.get()
_start_col_no = self.position_frames['start'].column.slider.get()
self.start_arg = "{}.{}".format(_start_line_no, _start_col_no)
if self._is_two_args.var.get():
_end_line_no = self.position_frames['end'].line.slider.get()
_end_col_no = self.position_frames['end'].column.slider.get()
self.end_arg = "{}.{}".format(_end_line_no, _end_col_no)
else:
self.end_arg = None
self._update_method_labels()
self._select()
def _update_method_labels(self):
if self.end_arg:
for _key in self.position_frames:
_string = "text.get('{}', '{}')".format(
self.start_arg, self.end_arg)
self.position_frames[_key].label['text'] = _string
else:
_string = "text.get('{}')".format(self.start_arg)
self.position_frames['start'].label['text'] = _string
def _select(self):
self.text.focus_set()
self.text.tag_remove('sel', '1.0', 'end')
self.text.tag_add('sel', self.start_arg, self.end_arg)
if self.end_arg:
self.text.mark_set('insert', self.end_arg)
else:
self.text.mark_set('insert', self.start_arg)
class TextWithStats(tk.Text):
"""
Text widget that stores stats of its content:
self.line_count: the total number of lines
self.lines_length: the total number of characters per line
self.update_callback: can be set as the reference to the callback
to be called with each update
"""
def __init__(self, master, update_callback=None, *args, **kwargs):
tk.Text.__init__(self, master, *args, **kwargs)
self._events = ('<KeyPress>',
'<KeyRelease>',
'<ButtonRelease-1>',
'<ButtonRelease-2>',
'<ButtonRelease-3>',
'<Delete>',
'<<Cut>>',
'<<Paste>>',
'<<Undo>>',
'<<Redo>>')
self.line_count = None
self.lines_length = list()
self.update_callback = update_callback
self.update_stats()
self.bind_events_on_widget_to_callback( self._events,
self,
self.update_stats)
#staticmethod
def bind_events_on_widget_to_callback(events, widget, callback):
"""
Bind events on widget to callback.
"""
for _event in events:
widget.bind(_event, callback)
def update_stats(self, event=None):
"""
Update self.line_count, self.lines_length stats and call
self.update_callback.
"""
_string = self.get('1.0', 'end-1c')
_string_lines = _string.splitlines()
self.line_count = len(_string_lines)
del self.lines_length[:]
for _line in _string_lines:
self.lines_length.append(len(_line))
if self.update_callback:
self.update_callback()
class PositionFrame(tk.LabelFrame):
"""
A LabelFrame that has two LabelFrames which has Scales.
"""
def __init__(self, master, *args, **kwargs):
tk.LabelFrame.__init__(self, master, *args, **kwargs)
self._create_widgets()
self._layout()
def _create_widgets(self):
self.line = SliderFrame(self, orient='vertical', text="line=")
self.column = SliderFrame(self, orient='horizontal', text="column=")
self.label = tk.Label(self, text="Label")
def _layout(self):
self.line.grid(sticky='ns', row=0, column=0, rowspan=2)
self.column.grid(sticky='ew', row=0, column=1, columnspan=2)
self.label.grid(sticky='nsew', row=1, column=1)
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure(1, weight=1)
class SliderFrame(tk.LabelFrame):
"""
A LabelFrame that encapsulates a Scale.
"""
def __init__(self, master, orient, *args, **kwargs):
tk.LabelFrame.__init__(self, master, *args, **kwargs)
self.slider = tk.Scale(self, orient=orient)
self.slider.pack(fill='both', expand=True)
if __name__ == '__main__':
root = tk.Tk()
demo = Demo(root, text="text.get(start, end=None)")
with open(__file__) as f:
demo.text.insert('1.0', f.read())
demo.text.update_stats()
demo.pack(fill='both', expand=True)
root.mainloop()
I think this is a better way-
variable1=StringVar() # Value saved here
def search():
print(variable1.get())
return ''
ttk.Entry(mainframe, width=7, textvariable=variable1).grid(column=2, row=1)
ttk.Label(mainframe, text="label").grid(column=1, row=1)
ttk.Button(mainframe, text="Search", command=search).grid(column=2, row=13)
On pressing the button, the value in the text field would get printed.
But make sure You import the ttk separately.
The full code for a basic application is-
from tkinter import *
from tkinter import ttk
root=Tk()
mainframe = ttk.Frame(root, padding="10 10 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
variable1=StringVar() # Value saved here
def search():
print(variable1.get())
return ''
ttk.Entry(mainframe, width=7, textvariable=variable1).grid(column=2, row=1)
ttk.Label(mainframe, text="label").grid(column=1, row=1)
ttk.Button(mainframe, text="Search", command=search).grid(column=2, row=13)
root.mainloop()
I faced the problem of gettng entire text from Text widget and following solution worked for me :
txt.get(1.0,END)
Where 1.0 means first line, zeroth character (ie before the first!)
is the starting position and END is the ending position.
Thanks to Alan Gauld in this link
I did come also in search of how to get input data from the Text widget. Regarding the problem with a new line on the end of the string. You can just use .strip() since it is a Text widget that is always a string.
Also, I'm sharing code where you can see how you can create multiply Text widgets and save them in the dictionary as form data, and then by clicking the submit button get that form data and do whatever you want with it. I hope it helps others. It should work in any 3.x python and probably will work in 2.7 also.
from tkinter import *
from functools import partial
class SimpleTkForm(object):
def __init__(self):
self.root = Tk()
def myform(self):
self.root.title('My form')
frame = Frame(self.root, pady=10)
form_data = dict()
form_fields = ['username', 'password', 'server name', 'database name']
cnt = 0
for form_field in form_fields:
Label(frame, text=form_field, anchor=NW).grid(row=cnt,column=1, pady=5, padx=(10, 1), sticky="W")
textbox = Text(frame, height=1, width=15)
form_data.update({form_field: textbox})
textbox.grid(row=cnt,column=2, pady=5, padx=(3,20))
cnt += 1
conn_test = partial(self.test_db_conn, form_data=form_data)
Button(frame, text='Submit', width=15, command=conn_test).grid(row=cnt,column=2, pady=5, padx=(3,20))
frame.pack()
self.root.mainloop()
def test_db_conn(self, form_data):
data = {k:v.get('1.0', END).strip() for k,v in form_data.items()}
# validate data or do anything you want with it
print(data)
if __name__ == '__main__':
api = SimpleTkForm()
api.myform()
I would argue that creating a simple extension of Text and turning text into a property is the cleanest way to go. You can then stick that extension in some file that you always import, and use it instead of the original Text widget. This way, instead of having to remember, write, repeat, etc all the hoops tkinter makes you jump through to do the simplest things, you have a butt-simple interface that can be reused in any project. You can do this for Entry, as well, but the syntax is slightly different.
import tkinter as tk
root = tk.Tk()
class Text(tk.Text):
#property
def text(self) -> str:
return self.get('1.0', 'end-1c')
#text.setter
def text(self, value) -> None:
self.replace('1.0', 'end-1c', value)
def __init__(self, master, **kwargs):
tk.Text.__init__(self, master, **kwargs)
#Entry version of the same concept as above
class Entry(tk.Entry):
#property
def text(self) -> str:
return self.get()
#text.setter
def text(self, value) -> None:
self.delete(0, 'end')
self.insert(0, value)
def __init__(self, master, **kwargs):
tk.Entry.__init__(self, master, **kwargs)
textbox = Text(root)
textbox.grid()
textbox.text = "this is text" #set
print(textbox.text) #get
entry = Entry(root)
entry.grid()
entry.text = 'this is text' #set
print(entry.text) #get
root.mainloop()
Lets say that you have a Text widget called my_text_widget.
To get input from the my_text_widget you can use the get function.
Let's assume that you have imported tkinter.
Lets define my_text_widget first, lets make it just a simple text widget.
my_text_widget = Text(self)
To get input from a text widget you need to use the get function, both, text and entry widgets have this.
input = my_text_widget.get()
The reason we save it to a variable is to use it in the further process, for example, testing for what's the input.
I'm creating a grid of Frames from a list of lists. I have a Frame class that has a popup menu bound to Button-1. I'd like to use this pop-up menu to update the label inside the Frame that called the popup menu. Right now only the last Frame's label text is getting updated.
I've tried playing around with bind tags, as I thought maybe the Button-1 is getting bound to only the most recently created widget, but it seems like this was a red herring. I'm actually totally stumped on where to go from here.
#!/bin/env python
import tkinter
import tkinter.messagebox
def create_grid():
grid = []
for i in range(0, 3001, 1000):
row = []
for j in range(8):
row.append(i+j)
grid.append(row)
grid.reverse()
return grid
class GridFrame(tkinter.Frame):
def __init__(self, root, index, *args, **kwargs):
tkinter.Frame.__init__(self, root, *args, **kwargs)
self.labeltext = tkinter.StringVar()
self.labeltext.set("+")
self.popup_menu = tkinter.Menu(root, tearoff=0)
self.popup_menu.add_command(label="Set label to 'test'", command=self.update_label)
self.popup_menu.add_command(label="Do nothing", command=print)
self.bind_all("<Button-1>", self.popup)
self.bind_all("<Button-3>", self.reset_label)
self.index_label = tkinter.Label(self, text="{0:04d}".format(index))
self.index_label.pack()
self.framelabel = tkinter.Label(self, textvariable=self.labeltext)
self.framelabel.pack()
def popup(self, event):
try:
self.popup_menu.tk_popup(event.x_root, event.y_root, 0)
finally:
self.popup_menu.grab_release()
def reset_label(self, event):
self.labeltext.set("+")
def update_label(self):
self.labeltext.set("test")
class GridGUI:
def __init__(self, root, grid, *args, **kwargs):
self.root = root
root.title("Grid")
for i, row in enumerate(grid):
for j, index in enumerate(row):
gridframe = GridFrame(root, index)
gridframe.config(borderwidth=3, relief="raised")
gridframe.grid(row=i, column=j, padx=2, pady=2, ipadx=20, ipady=30, sticky="nsew")
def main():
grid = create_grid()
root = tkinter.Tk()
menubar = tkinter.Menu(root)
filemenu = tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
my_gui = GridGUI(root, grid)
root.mainloop()
main()
I expect the label in the Frame the popup menu originates would update. With my current code it seems only the label in the most recently created Frame updates.
Your problem is that you're using bind_all, and that you're resetting that binding every time you create a new GridFrame. Thus, only the binding to the last GridFrame will be the one recognized by tkinter. When tkinter detects the binding, it calls the popup method of the final GridFrame.
You can work around this since the event object tells you which widget was clicked on. From that, you can determine which GridFrame instance was clicked on. However, there is a simpler solution.
The solution is to not use bind_all, and instead bind to each widget individually. When the binding fires, it will then call the function associated with the appropriate object.
After creating the widgets, apply a binding to every widget:
class GridFrame(tkinter.Frame):
def __init__(self, root, index, *args, **kwargs):
...
for widget in (self, self.index_label, self.framelabel):
widget.bind("<Button-1>", self.popup)
This has the added benefit that you can have any other widgets on the screen that accept button clicks (buttons, scrollbars, etc) without having them be affected by the global binding to a mouse click.
If you don't mind ugly hacks, here's one way to do it. Instead of creating an individual popup menu within each frame, create only one in your main GUI, check for the popup location, and alter the corresponding StringVar.
import tkinter
import tkinter.messagebox
y = []
def create_grid():
grid_list = []
for i in range(0, 3001, 1000):
row = []
for j in range(8):
row.append(i+j)
grid_list.append(row)
grid_list.reverse()
return grid_list
class GridFrame(tkinter.Frame):
def __init__(self, root, index, num, *args, **kwargs):
tkinter.Frame.__init__(self, root, *args, **kwargs,name=f"{num}")
self.labeltext = tkinter.StringVar()
y.append(self.labeltext)
self.labeltext.set("+")
self.index_label = tkinter.Label(self, text="{0:04d}".format(index))
self.index_label.pack()
self.framelabel = tkinter.Label(self, textvariable=self.labeltext)
self.framelabel.pack()
class GridGUI:
def __init__(self, root, grid_list, *args, **kwargs):
root.title("Grid")
num = 0
for i, row in enumerate(grid_list):
for j, index in enumerate(row):
gridframe = GridFrame(root, index, num)
gridframe.config(borderwidth=3, relief="raised")
gridframe.grid(row=i, column=j, padx=2, pady=2, ipadx=20, ipady=30, sticky="nsew")
num+=1
self.popup_menu = tkinter.Menu(root, tearoff=0)
self.popup_menu.add_command(label="Set label to 'test'", command=lambda: self.update_label(self.event))
self.popup_menu.add_command(label="Do nothing", command=lambda: self.reset_label(self.event))
root.bind_all("<Button-1>", self.popup)
root.bind_all("<Button-3>", self.reset_label)
def popup(self, event):
self.event = event
try:
self.popup_menu.tk_popup(event.x_root, event.y_root, 0)
finally:
self.popup_menu.grab_release()
def reset_label(self, event):
result = str(event.widget).split(".")[1]
y[int(result)].set("+")
def update_label(self,event):
result = str(event.widget).split(".")[1]
y[int(result)].set("test")
def main():
grid = create_grid()
root = tkinter.Tk()
menubar = tkinter.Menu(root)
filemenu = tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
my_gui = GridGUI(root, grid)
root.mainloop()
main()
I'm having trouble with using the Entry box widget in tkinter. I want to have a new window to open when the user selects Edit -> Backbone... . In this window there will be a number of Entry widgets (just one coded for below for simplicity) that show default Strings stored in various instances the class Elements. The user should be able to edit this string and save it by clicking OK and returning it to its default value by clicking default. The entry box should always show the current value of the variable each time the backbone editor is reopened (If the whole program is restarted it does not need to remember the user input).
Upon opening the 'Backbone Editor' window the Entry box should show the string text variable but I can't make it appear.
from tkinter import *
from tkinter import ttk
class View(ttk.Frame):
"""Main GUI class"""
def __init__(self, master = None):
self.WIDTH = 450
self.HEIGHT = 500
self.lib = MolecularLibrary()
# Set up the main window
ttk.Frame.__init__(self, master, borderwidth=5, width=self.WIDTH, height=self.WIDTH)
self.master.resizable(FALSE, FALSE)
self.grid(column=0, row=0, sticky=(N, S, E, W))
self.columnconfigure(0, weight=1)
self.create_menus()
def create_menus(self):
"""Produces the menu layout for the main window"""
self.master.option_add('*tearOff', FALSE)
self.menubar = Menu(self.master)
self.master['menu'] = self.menubar
# Menu Variables
menu_edit = Menu(self.menubar)
# Add the menus to the menubar and assign their variables
self.menubar.add_cascade(menu=menu_edit, label = "Edit")
### Edit ###
menu_edit.add_command(label="Backbone...", command=lambda : self.edit_backbone())
def edit_backbone(self):
"""Shows a window where the backbone constituents can be edited"""
backbone_window = Toplevel(borderwidth = 5)
backbone_window.title("Backbone Editor")
backbone_window.resizable(FALSE, FALSE)
print("sugar value", self.lib.sugar_var)
# LABELS FOR BACKBONE #
# Phosphate annotations and input
sugar_label = ttk.Label(backbone_window, text = "Sugar")
#inputs
sugar = ttk.Entry(backbone_window, textvariable = self.lib.sugar_var, justify = 'center', width=10)
### Buttons ###
default = ttk.Button(backbone_window, text = "Defaults", command=lambda : defaults())
okay = ttk.Button(backbone_window, text = "Okay", command=lambda : okay())
cancel = ttk.Button(backbone_window, text = "Cancel", command=lambda : backbone_window.destroy())
#content.grid(column=0, row=0)
sugar_label.grid(column=2, row=1)
sugar.grid(column=1, row=2, columnspan=3)
default.grid(column=0, row=12, columnspan=3, pady=2)
okay.grid(column=6, row=12, columnspan=3, pady=2)
cancel.grid(column=9, row=12, columnspan=4, pady=2)
backbone_window.focus()
def defaults():
"""Reset the backbone and bases to their defaults."""
self.lib.set_molecules()
def okay():
"""Set the backbone and base variables to the user set values."""
self.lib.sugar_var.new_formula(sugar.get())
backbone_window.destroy()
class MolecularLibrary:
"""
"""
def __init__(self, atom_file = r'C:\MyPyProgs\OSeq\resources\ATOMS.txt',
precision = 4):
self.molecules = {}
self.atom_file = atom_file
# self.molecule_file = molecule_file
# Variables
self.set_molecules()
def set_molecules(self):
"""
Set all of the molecules for the backbone and bases to their default values and forumlae.
"""
### Sugar ###
self.sugar_var = Element('C5H8O3', 'A')
def add_molecule(self, molecule):
"""(MolecularLibrary, list) -> None
Returns a dictionary of the molecule name as an Element
{molecule[0]: Element}
"""
print(molecule)
tmp = self.get_mass(molecule[1])
return {molecule[0]: Element(molecule[1], molecule[0], tmp[0], tmp[0])}
class Element:
"""
Creates an element with the following construct:
[symbol, name, monoisotopic, average]
"""
def __init__(self, symbol, name):
self.symbol = symbol
self.name = name
def __str__(self):
return str([self.symbol, self.name])
def get_name(self):
"""Returns the name of the Element"""
return self.name
def get_symbol(self):
"""Returns the symbol of the Element"""
return self.symbol
def new_formula(self, new_formula):
"""replace the formula with new_formaula and recalculate the
average and monoisotopic masses."""
self.symbol = new_formula
if __name__ == "__main__":
root = Tk()
root.title("Sequencer")
view = View(root)
root.mainloop()
The code above is a heavily stripped version of my program but has the same basic architecture. I'm afraid there is still quite a lot of code, I would normally try strip it down more, but I'm not sure if the issue I'm having is from the architecture or not.
In order to use the textvariable attribute, you must give it an instance of a tkinter variable: StringVar, IntVar, BooleanVar, or DoubleVar
A good starting point to learn more about these variables is here: http://effbot.org/tkinterbook/variable.htm
How to get Tkinter input from the Text widget?
EDIT
I asked this question to help others with the same problem - that is the reason why there is no example code. This issue had been troubling me for hours and I used this question to teach others. Please do not rate it as if it was a real question - the answer is the thing that matters.
To get Tkinter input from the text box, you must add a few more attributes to the normal .get() function. If we have a text box myText_Box, then this is the method for retrieving its input.
def retrieve_input():
input = self.myText_Box.get("1.0",END)
The first part, "1.0" means that the input should be read from line one, character zero (ie: the very first character). END is an imported constant which is set to the string "end". The END part means to read until the end of the text box is reached. The only issue with this is that it actually adds a newline to our input. So, in order to fix it we should change END to end-1c(Thanks Bryan Oakley) The -1c deletes 1 character, while -2c would mean delete two characters, and so on.
def retrieve_input():
input = self.myText_Box.get("1.0",'end-1c')
Here is how I did it with python 3.5.2:
from tkinter import *
root=Tk()
def retrieve_input():
inputValue=textBox.get("1.0","end-1c")
print(inputValue)
textBox=Text(root, height=2, width=10)
textBox.pack()
buttonCommit=Button(root, height=1, width=10, text="Commit",
command=lambda: retrieve_input())
#command=lambda: retrieve_input() >>> just means do this when i press the button
buttonCommit.pack()
mainloop()
with that, when i typed "blah blah" in the text widget and pressed the button, whatever i typed got printed out. So i think that is the answer for storing user input from Text widget to variable.
To get Tkinter input from the text box in python 3 the complete student level program used by me is as under:
#Imports all (*) classes,
#atributes, and methods of tkinter into the
#current workspace
from tkinter import *
#***********************************
#Creates an instance of the class tkinter.Tk.
#This creates what is called the "root" window. By conventon,
#the root window in Tkinter is usually called "root",
#but you are free to call it by any other name.
root = Tk()
root.title('how to get text from textbox')
#**********************************
mystring = StringVar()
####define the function that the signup button will do
def getvalue():
## print(mystring.get())
#*************************************
Label(root, text="Text to get").grid(row=0, sticky=W) #label
Entry(root, textvariable = mystring).grid(row=0, column=1, sticky=E) #entry textbox
WSignUp = Button(root, text="print text", command=getvalue).grid(row=3, column=0, sticky=W) #button
############################################
# executes the mainloop (that is, the event loop) method of the root
# object. The mainloop method is what keeps the root window visible.
# If you remove the line, the window created will disappear
# immediately as the script stops running. This will happen so fast
# that you will not even see the window appearing on your screen.
# Keeping the mainloop running also lets you keep the
# program running until you press the close buton
root.mainloop()
In order to obtain the string in a Text widget one can simply use get method defined for Text which accepts 1 to 2 arguments as start and end positions of characters, text_widget_object.get(start, end=None). If only start is passed and end isn't passed it returns only the single character positioned at start, if end is passed as well, it returns all characters in between positions start and end as string.
There are also special strings, that are variables to the underlying Tk. One of them would be "end" or tk.END which represents the variable position of the very last char in the Text widget. An example would be to returning all text in the widget, with text_widget_object.get('1.0', 'end') or text_widget_object.get('1.0', 'end-1c') if you don't want the last newline character.
Demo
See below demonstration that selects the characters in between the given positions with sliders:
try:
import tkinter as tk
except:
import Tkinter as tk
class Demo(tk.LabelFrame):
"""
A LabeFrame that in order to demonstrate the string returned by the
get method of Text widget, selects the characters in between the
given arguments that are set with Scales.
"""
def __init__(self, master, *args, **kwargs):
tk.LabelFrame.__init__(self, master, *args, **kwargs)
self.start_arg = ''
self.end_arg = None
self.position_frames = dict()
self._create_widgets()
self._layout()
self.update()
def _create_widgets(self):
self._is_two_args = tk.Checkbutton(self,
text="Use 2 positional arguments...")
self.position_frames['start'] = PositionFrame(self,
text="start='{}.{}'.format(line, column)")
self.position_frames['end'] = PositionFrame( self,
text="end='{}.{}'.format(line, column)")
self.text = TextWithStats(self, wrap='none')
self._widget_configs()
def _widget_configs(self):
self.text.update_callback = self.update
self._is_two_args.var = tk.BooleanVar(self, value=False)
self._is_two_args.config(variable=self._is_two_args.var,
onvalue=True, offvalue=False)
self._is_two_args['command'] = self._is_two_args_handle
for _key in self.position_frames:
self.position_frames[_key].line.slider['command'] = self.update
self.position_frames[_key].column.slider['command'] = self.update
def _layout(self):
self._is_two_args.grid(sticky='nsw', row=0, column=1)
self.position_frames['start'].grid(sticky='nsew', row=1, column=0)
#self.position_frames['end'].grid(sticky='nsew', row=1, column=1)
self.text.grid(sticky='nsew', row=2, column=0,
rowspan=2, columnspan=2)
_grid_size = self.grid_size()
for _col in range(_grid_size[0]):
self.grid_columnconfigure(_col, weight=1)
for _row in range(_grid_size[1] - 1):
self.grid_rowconfigure(_row + 1, weight=1)
def _is_two_args_handle(self):
self.update_arguments()
if self._is_two_args.var.get():
self.position_frames['end'].grid(sticky='nsew', row=1, column=1)
else:
self.position_frames['end'].grid_remove()
def update(self, event=None):
"""
Updates slider limits, argument values, labels representing the
get method call.
"""
self.update_sliders()
self.update_arguments()
def update_sliders(self):
"""
Updates slider limits based on what's written in the text and
which line is selected.
"""
self._update_line_sliders()
self._update_column_sliders()
def _update_line_sliders(self):
if self.text.lines_length:
for _key in self.position_frames:
self.position_frames[_key].line.slider['state'] = 'normal'
self.position_frames[_key].line.slider['from_'] = 1
_no_of_lines = self.text.line_count
self.position_frames[_key].line.slider['to'] = _no_of_lines
else:
for _key in self.position_frames:
self.position_frames[_key].line.slider['state'] = 'disabled'
def _update_column_sliders(self):
if self.text.lines_length:
for _key in self.position_frames:
self.position_frames[_key].column.slider['state'] = 'normal'
self.position_frames[_key].column.slider['from_'] = 0
_line_no = int(self.position_frames[_key].line.slider.get())-1
_max_line_len = self.text.lines_length[_line_no]
self.position_frames[_key].column.slider['to'] = _max_line_len
else:
for _key in self.position_frames:
self.position_frames[_key].column.slider['state'] = 'disabled'
def update_arguments(self):
"""
Updates the values representing the arguments passed to the get
method, based on whether or not the 2nd positional argument is
active and the slider positions.
"""
_start_line_no = self.position_frames['start'].line.slider.get()
_start_col_no = self.position_frames['start'].column.slider.get()
self.start_arg = "{}.{}".format(_start_line_no, _start_col_no)
if self._is_two_args.var.get():
_end_line_no = self.position_frames['end'].line.slider.get()
_end_col_no = self.position_frames['end'].column.slider.get()
self.end_arg = "{}.{}".format(_end_line_no, _end_col_no)
else:
self.end_arg = None
self._update_method_labels()
self._select()
def _update_method_labels(self):
if self.end_arg:
for _key in self.position_frames:
_string = "text.get('{}', '{}')".format(
self.start_arg, self.end_arg)
self.position_frames[_key].label['text'] = _string
else:
_string = "text.get('{}')".format(self.start_arg)
self.position_frames['start'].label['text'] = _string
def _select(self):
self.text.focus_set()
self.text.tag_remove('sel', '1.0', 'end')
self.text.tag_add('sel', self.start_arg, self.end_arg)
if self.end_arg:
self.text.mark_set('insert', self.end_arg)
else:
self.text.mark_set('insert', self.start_arg)
class TextWithStats(tk.Text):
"""
Text widget that stores stats of its content:
self.line_count: the total number of lines
self.lines_length: the total number of characters per line
self.update_callback: can be set as the reference to the callback
to be called with each update
"""
def __init__(self, master, update_callback=None, *args, **kwargs):
tk.Text.__init__(self, master, *args, **kwargs)
self._events = ('<KeyPress>',
'<KeyRelease>',
'<ButtonRelease-1>',
'<ButtonRelease-2>',
'<ButtonRelease-3>',
'<Delete>',
'<<Cut>>',
'<<Paste>>',
'<<Undo>>',
'<<Redo>>')
self.line_count = None
self.lines_length = list()
self.update_callback = update_callback
self.update_stats()
self.bind_events_on_widget_to_callback( self._events,
self,
self.update_stats)
#staticmethod
def bind_events_on_widget_to_callback(events, widget, callback):
"""
Bind events on widget to callback.
"""
for _event in events:
widget.bind(_event, callback)
def update_stats(self, event=None):
"""
Update self.line_count, self.lines_length stats and call
self.update_callback.
"""
_string = self.get('1.0', 'end-1c')
_string_lines = _string.splitlines()
self.line_count = len(_string_lines)
del self.lines_length[:]
for _line in _string_lines:
self.lines_length.append(len(_line))
if self.update_callback:
self.update_callback()
class PositionFrame(tk.LabelFrame):
"""
A LabelFrame that has two LabelFrames which has Scales.
"""
def __init__(self, master, *args, **kwargs):
tk.LabelFrame.__init__(self, master, *args, **kwargs)
self._create_widgets()
self._layout()
def _create_widgets(self):
self.line = SliderFrame(self, orient='vertical', text="line=")
self.column = SliderFrame(self, orient='horizontal', text="column=")
self.label = tk.Label(self, text="Label")
def _layout(self):
self.line.grid(sticky='ns', row=0, column=0, rowspan=2)
self.column.grid(sticky='ew', row=0, column=1, columnspan=2)
self.label.grid(sticky='nsew', row=1, column=1)
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure(1, weight=1)
class SliderFrame(tk.LabelFrame):
"""
A LabelFrame that encapsulates a Scale.
"""
def __init__(self, master, orient, *args, **kwargs):
tk.LabelFrame.__init__(self, master, *args, **kwargs)
self.slider = tk.Scale(self, orient=orient)
self.slider.pack(fill='both', expand=True)
if __name__ == '__main__':
root = tk.Tk()
demo = Demo(root, text="text.get(start, end=None)")
with open(__file__) as f:
demo.text.insert('1.0', f.read())
demo.text.update_stats()
demo.pack(fill='both', expand=True)
root.mainloop()
I think this is a better way-
variable1=StringVar() # Value saved here
def search():
print(variable1.get())
return ''
ttk.Entry(mainframe, width=7, textvariable=variable1).grid(column=2, row=1)
ttk.Label(mainframe, text="label").grid(column=1, row=1)
ttk.Button(mainframe, text="Search", command=search).grid(column=2, row=13)
On pressing the button, the value in the text field would get printed.
But make sure You import the ttk separately.
The full code for a basic application is-
from tkinter import *
from tkinter import ttk
root=Tk()
mainframe = ttk.Frame(root, padding="10 10 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
variable1=StringVar() # Value saved here
def search():
print(variable1.get())
return ''
ttk.Entry(mainframe, width=7, textvariable=variable1).grid(column=2, row=1)
ttk.Label(mainframe, text="label").grid(column=1, row=1)
ttk.Button(mainframe, text="Search", command=search).grid(column=2, row=13)
root.mainloop()
I faced the problem of gettng entire text from Text widget and following solution worked for me :
txt.get(1.0,END)
Where 1.0 means first line, zeroth character (ie before the first!)
is the starting position and END is the ending position.
Thanks to Alan Gauld in this link
I did come also in search of how to get input data from the Text widget. Regarding the problem with a new line on the end of the string. You can just use .strip() since it is a Text widget that is always a string.
Also, I'm sharing code where you can see how you can create multiply Text widgets and save them in the dictionary as form data, and then by clicking the submit button get that form data and do whatever you want with it. I hope it helps others. It should work in any 3.x python and probably will work in 2.7 also.
from tkinter import *
from functools import partial
class SimpleTkForm(object):
def __init__(self):
self.root = Tk()
def myform(self):
self.root.title('My form')
frame = Frame(self.root, pady=10)
form_data = dict()
form_fields = ['username', 'password', 'server name', 'database name']
cnt = 0
for form_field in form_fields:
Label(frame, text=form_field, anchor=NW).grid(row=cnt,column=1, pady=5, padx=(10, 1), sticky="W")
textbox = Text(frame, height=1, width=15)
form_data.update({form_field: textbox})
textbox.grid(row=cnt,column=2, pady=5, padx=(3,20))
cnt += 1
conn_test = partial(self.test_db_conn, form_data=form_data)
Button(frame, text='Submit', width=15, command=conn_test).grid(row=cnt,column=2, pady=5, padx=(3,20))
frame.pack()
self.root.mainloop()
def test_db_conn(self, form_data):
data = {k:v.get('1.0', END).strip() for k,v in form_data.items()}
# validate data or do anything you want with it
print(data)
if __name__ == '__main__':
api = SimpleTkForm()
api.myform()
I would argue that creating a simple extension of Text and turning text into a property is the cleanest way to go. You can then stick that extension in some file that you always import, and use it instead of the original Text widget. This way, instead of having to remember, write, repeat, etc all the hoops tkinter makes you jump through to do the simplest things, you have a butt-simple interface that can be reused in any project. You can do this for Entry, as well, but the syntax is slightly different.
import tkinter as tk
root = tk.Tk()
class Text(tk.Text):
#property
def text(self) -> str:
return self.get('1.0', 'end-1c')
#text.setter
def text(self, value) -> None:
self.replace('1.0', 'end-1c', value)
def __init__(self, master, **kwargs):
tk.Text.__init__(self, master, **kwargs)
#Entry version of the same concept as above
class Entry(tk.Entry):
#property
def text(self) -> str:
return self.get()
#text.setter
def text(self, value) -> None:
self.delete(0, 'end')
self.insert(0, value)
def __init__(self, master, **kwargs):
tk.Entry.__init__(self, master, **kwargs)
textbox = Text(root)
textbox.grid()
textbox.text = "this is text" #set
print(textbox.text) #get
entry = Entry(root)
entry.grid()
entry.text = 'this is text' #set
print(entry.text) #get
root.mainloop()
Lets say that you have a Text widget called my_text_widget.
To get input from the my_text_widget you can use the get function.
Let's assume that you have imported tkinter.
Lets define my_text_widget first, lets make it just a simple text widget.
my_text_widget = Text(self)
To get input from a text widget you need to use the get function, both, text and entry widgets have this.
input = my_text_widget.get()
The reason we save it to a variable is to use it in the further process, for example, testing for what's the input.
In the app I'm working on I have a class setup to handle all my GUI. One method in this class creates a frame and populates a group of radiobutton's from a loop. This method will be called multiple times from outside the class to redraw this frame. The problem I have is that when the frame is redrawn it is actually just overwriting the existing frame (old frame persists). So if there are fewer options in a later call, the earlier options are still visible. I have tried doing a grid_remove on the frame first but can't get that to work. So my question is, why is the grid_remove() not working in the initMech() method below?
#!/usr/bin/env python
from Tkinter import *
class MWindow(Frame):
def __init__(self, parent) :
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
topframe = Frame(self.parent, bd=2, relief=GROOVE, padx=5, pady=5, width=300, height=50)
topframe.grid(row=0, column=0)
desc = Button(topframe, text="list1", command=lambda:set_list(1))
desc.grid(row=0,column=1)
desc2 = Button(topframe, text="list2", command=lambda:set_list(2))
desc2.grid(row=0,column=2)
def initMech(self):
try:
radio_frame.grid_remove()
except:
print "can't remove"
radio_frame = Frame(self.parent, bd=2, relief=GROOVE, padx=5, pady=5, width=300, height=50)
radio_frame.grid(row=1, column=0)
variant=StringVar()
c = 0
for x in chas.v_list:
Radiobutton(radio_frame, text=x,variable=variant, value=x, command=lambda x = x:chas.set_vari(x)).grid(row=0, column=c)
c = c+1
def onExit(self):
self.parent.destroy()
class Mech():
def set_chass(self,chass):
try:
if self.chassis == chass:
pass
else:
self.chassis = chass
del self.vari
except AttributeError:
self.chassis = chass
def load_vari(self):
if self.chassis == 1:
self.v_list = ["a","b","c"]
else:
self.v_list = ["w", "x", "y", "z"]
win.initMech()
def set_vari(self, vari):
self.vari = vari
def set_list(num):
chas.set_chass(num)
chas.load_vari()
root = Tk()
win = MWindow(root)
chas = Mech()
root.mainloop()
It is not working because you're using a local variable named radio_frame, and that variable isn't defined yet. Most likely, the error message you are ignoring is telling you exactly that. Why are you trying to remove a frame that you haven't yet created?
Also, are you aware that grid_remove only removes the widget from view, it doesn't destroy it? Thus, if you call this function several times you'll have several invisible versions of this frame, all using up memory.