Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the text out of a scrolledtext widget?

I am new to the tkinter python module. I try to do a project. I learned something new about the menus and I'm trying to make a little UI project that allows the user to write something in a scrolled text widget and then save it (using the sys module).

I've already tried some things that worked at buttons. For example .get() but it didn't work. I also tried the ["text"] method.

import tkinter, sys

root = tkinter.Tk()

class saveProject:
    def __init__(self, master):
        self.master = master
        self.textFrame = tkinter.scrolledtext.ScrolledText(self.master, width=100, bd=10, relief="raised")
        self.textFrame.pack()
     def save(self):
        #self.saveText = self.textFrame.get()
        self.saveText = self.textFrame["text"]

project = saveProject(root)
root.mainloop()

The problem is, as I already said, I don't know how to get the text out of a tkinter.scrolledtext.ScrolledText widget.

like image 341
David Avatar asked Dec 26 '18 21:12

David


2 Answers

So, out of curiosity I tried what described here (same link in my comment to the OP question). It works also for the scrolledtext.

import tkinter, sys
from tkinter import scrolledtext

root = tkinter.Tk()

class saveProject:
    def __init__(self, master):
        self.master = master
        self.textFrame = scrolledtext.ScrolledText(self.master, width=100, bd=10, relief="raised")
        self.textFrame.pack()
        self.saveb = tkinter.Button(self.master, text="Save", command= lambda : self.save())
        self.saveb.pack()

    def save(self):
        cur_inp = self.textFrame.get("1.0", tkinter.END)
        fl = open("output.txt", "w")
        fl.write(cur_inp)

project = saveProject(root)
root.mainloop()

I've added a save button at the bottom of the ScrolledText widget. The widget content is saved inside the output.txt area.

like image 180
Valentino Avatar answered Nov 02 '22 05:11

Valentino


help(ScrolledText) indicates it's a subclass of the tkinter.Text widget, which apparently means that the way to get the text from it is the same — via its get() method using "Text widget indices" (here's some documentation about them).

Below is an example that gets all of the text in the widget (I added a Save text Button to test the save() method):

import sys
import tkinter as tk
from tkinter.scrolledtext import ScrolledText

class SaveProject:
    def __init__(self, master):
        self.master = master
        self.textFrame = ScrolledText(self.master, width=100, bd=10, relief="raised")
        self.textFrame.pack()
        # Added for testing.
        self.save_btn = tk.Button(self.master, text='Save text', command=self.save)
        self.save_btn.pack()

    def save(self):
        self.saveText = self.textFrame.get('1.0', tk.END)  # Get all text in widget.
        print('self.saveText:', self.saveText)

root = tk.Tk()
project = SaveProject(root)
root.mainloop()
like image 4
martineau Avatar answered Nov 02 '22 06:11

martineau