9

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
David
  • 624
  • 1
  • 6
  • 21
  • `tkinter` doesn't have a `scrolledtext.ScrolledText` or a `ScrolledText` widget that I know of—so it's unclear what you are talking about. It is possible to make a regular `Text` scrollable on the other hand. – martineau Dec 26 '18 at 23:35
  • @martineau have a look here: [https://docs.python.org/3/library/tkinter.scrolledtext.html] I think this answer might help you: [https://stackoverflow.com/questions/14824163/how-to-get-the-input-from-the-tkinter-text-box-widget]. Could this question be considered a duplicate? – Valentino Dec 26 '18 at 23:39
  • @Valentino: That first docs link in your comment results in: `404 Not Found`. The other one to a possible duplicate might be one — **if** that's what the OP is really talking about. – martineau Dec 26 '18 at 23:43
  • @martineau ops, let me try again with the link. [https://docs.python.org/3/library/tkinter.scrolledtext.html ]. If the scrolledtext Gigi is speaking about is this, it's just a tkinter.Text class with a scrollbar. – Valentino Dec 26 '18 at 23:48
  • @Valentino: That link works better. I haven't ever used the `tix` extension widgets before (and won 't since its use has been "Deprecated since version 3.6"). The documentation of this, like most everythng else in `tkinter`, isn't great either. I suppose one could look at the source code a figure-out how to do this... – martineau Dec 26 '18 at 23:57
  • @Valentino: BTW, the OP's code doesn't get very far the way it is: `AttributeError: module 'tkinter' has no attribute 'scrolledtext'`—so it has other problems, as well. – martineau Dec 27 '18 at 00:06
  • @martineau you're right. My guess is that the `scrolledtext` module is not loaded when you import tkinter. At least I need to explicitly add `from tkinter import scrolledtext` to make it working. However this not really answer the OP question. – Valentino Dec 27 '18 at 00:31

2 Answers2

7

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.

Valentino
  • 7,291
  • 6
  • 18
  • 34
5

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()
Wolf
  • 9,679
  • 7
  • 62
  • 108
martineau
  • 119,623
  • 25
  • 170
  • 301