ttkbootstrap icon indicating copy to clipboard operation
ttkbootstrap copied to clipboard

Add read-only state for Text widget and ScrolledText widget

Open antrrax opened this issue 2 years ago • 1 comments

Is your feature request related to a problem? Please describe.

Would it be possible, to add read-only state to Text widget and ScrolledText widget?

Describe the solution you'd like

See this post on the subject [New: Wrapping the text widget (another way)] https://wiki.tcl-lang.org/page/Read%2Donly+text+widget

Describe alternatives you've considered

# read only text widget, idea and code by emiliano and ccbbaa, tested: tcl/tk8.6, linux - version 20191217-0
namespace eval ::roText {
  proc roText {w args} {
    if {[info exists ::roText::$w]} {
      puts stderr "::roText::$w and possibly $w already exist"
      return ;# discuss: better way to flag error to caller, return "" for now
    }
    text $w {*}$args
    bind $w <Control-KeyPress-z> break ;# delete all
    bind $w <Control-KeyPress-k> break ;# kill line
    bind $w <Control-KeyPress-h> break ;# Backspace alternate
    bind $w <Control-KeyPress-d> break ;# Del alternate
    bind $w <Control-KeyPress-o> break ;# Ins newline
    bind $w <Key-Delete> break
    bind $w <Key-BackSpace> break
    rename $w ::roText::$w
    proc ::$w {cmd args} [format {
      set w %s
      set inf [lindex [info level 1] 0] ;# caller proc name; find tk vs scr.
      #puts "* $cmd $args ([info level]) '$inf'" ;# debug
      if {($cmd ni "insert delete") || ( ($cmd in "insert delete") \
        && ([string range $inf 0 3] != "tk::") \
        && ($inf != "tk_textCut") && ($inf != "tk_textPaste") ) \
      } {
        ::roText::$w $cmd {*}$args
      } 
    } $w $w]
    bind ::$w <Destroy> [list rename $w {}]
    return $w ;# created
  }
}

# usage test
roText::roText .txt ;# create
pack .txt
.txt insert end {Test
test
test}

Additional context

No response

antrrax avatar Jul 31 '22 23:07 antrrax

I ran into a problem while trying to .configure the "state" of the ScrolledText widget. I'm trying to disable user input into the widget but still be able to insert data. output_box = ScrolledText(output, padding=5, height=20, width=80, autohide=True, state='disabled')

  File "**\gui\main.py", line 50, in print_output
    self.output_box.configure(state='normal')
  File "**\venv\Lib\site-packages\ttkbootstrap\style.py", line 5003, in configure
    func(self, cnf, **kwargs)
  File "**\Python\Python311\Lib\tkinter\__init__.py", line 1702, in configure
    return self._configure('configure', cnf, kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "**\Python\Python311\Lib\tkinter\__init__.py", line 1692, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-state"

As I workaround I used the protected member "_text" (self._text = ttk.Text(self, padx=50, **kwargs)) in the ScrolledText widget. output_box._text.configure(state='normal')

I think adding the suggestion for a "read-only" state would avoid this problem.

DKNorad avatar Oct 29 '23 03:10 DKNorad