Issue
I have a simple GUI code using tkinter
in Jupyter
:
from tkinter import *
root = Tk()
text = Text(root, width = 40, height = 15)
text.pack()
root.mainloop()
How can I get user inputted text?
If I add text.get('1.0', 'end')
to the end of that code, it does not work, also when I add text.get('1.0', 'end')
to another cell it doesn't start executing until I close the root
window and after that it gives an error.
Like this:
In[1]: from tkinter import *
root = Tk()
text = Text(root, width = 40, height = 15)
text.pack()
root.mainloop()
In[2]: text.get('1.0', 'end')
In[2]
does not start executing until I close the Tk()
window, and after closing the window and start running In[2]
it gives this error:
TclError: invalid command name ".!text"
Solution
You cannot interact with a tkinter mainloop
from the ipython
interactive.
If you add a get text button, pressing it will retrieve the text widget content:
Like this:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root, width=40, height = 15)
text.pack()
tk.Button(root, text='get text', command=lambda: print(text.get('1.0', tk.END))).pack()
root.mainloop()
Answered By - Reblochon Masque
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.