Python Forum
[Tkinter] Toggling the Check button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Toggling the Check button
#1
Please, how can I wrap and unwrap the 'wrap' attribute of the Text Widget by toggle with the check button (Word Wrap) in the sub-menu of my Menu Widget. Once it's checked the xscrolledbar (horizontal) should hide with the attribute 'wrap' set to WORD and once unchecked the xscrolledbar should show/display with wrap attribute set to NONE.
Thanks.

from tkinter import *
from tkinter import ttk
import Pmw

class Notepad:
    def __init__(self):
        self.master = Tk()
        self.master.title("Untitled - Notepad")           
        self.master.geometry('600x450+361+150')
        self.build_Notepad()
        self.master.mainloop()

    def build_Notepad(self):
        # BUILD FILE MENU
        self.frame = ttk.Frame(self.master)
        self.frame.pack()
        self.menu = Menu(self.frame,takefocus=1)
        self.master.configure(menu=self.menu)           
        self.filemenu = Menu(self.menu,tearoff=False)

        # FORMAT MENU
        self.menu.add_cascade(label='Format',menu=self.filemenu)
        for i in ('Word Wrap','Font...'):
            self.wrap = BooleanVar()
            self.wrap.set(1)
            if i == 'Word Wrap':
                  self.filemenu.add_checkbutton(label=i,offvalue=0,variable=self.wrap,command=self.wrap_text)
            else:
                self.filemenu.add_command(label=i)

        #BUILD TEXT AREA AND SCROLLED BAR
        self.frame_2 = ttk.Frame(self.master)
        self.frame_2.pack(fill=BOTH, expand=True)
        self.scrollbar = ttk.Scrollbar(self.frame_2)
        self.scrollbar.pack(side=RIGHT, fill=Y, expand=False)
        self.scrollbar_2 = ttk.Scrollbar(self.master, orient=HORIZONTAL)
        self.scrollbar_2.pack(fill=X, expand=NO)        
        self.text = Text(self.frame_2,yscrollcommand=self.scrollbar.set,xscrollcommand=self.scrollbar_2.set,wrap=NONE)
        self.text.pack(side=LEFT,fill=BOTH,expand=True)
        self.scrollbar.configure(command=self.text.yview)
        self.scrollbar_2.configure(command=self.text.xview)

    def display_xview(self):
        self.scrollbar_2 = Scrollbar(self.master,command=self.text.xview,orient=HORIZONTAL)
        self.scrollbar_2.pack(fill=X,expand=NO)
        self.text.config(xscrollcommand=self.scrollbar_2.set)

    def wrap_text(self):
        self.use  = self.wrap.get()
        if self.use:
            self.text.config(wrap=NONE)
        else:
            self.text.config(wrap=WORD)

if __name__== "__main__":
    Notepad()
Reply
#2
You're really close- I made some changes to your callback:
    def wrap_text(self):
        self.use  = self.wrap.get()        
        if self.use:
            self.text.config(wrap=WORD)
            self.wrap.set(0)            
            
        else:
            self.text.config(wrap=NONE)           
            self.wrap.set(1)
Reply
#3
(Mar-03-2019, 11:13 PM)joe_momma Wrote: You're really close- I made some changes to your callback:
 def wrap_text(self): self.use = self.wrap.get() if self.use: self.text.config(wrap=WORD) self.wrap.set(0) else: self.text.config(wrap=NONE) self.wrap.set(1) 
Thank you very much for your reply.
But is there a way I can also toggle (show/display) with tkinter x-scrollbar (Horizontal) on click of the 'Word wrap' submenu.
Thanks again.
Reply
#4
there may be other ways to accomplish this but .pack_forget() works:
def wrap_text(self):
        self.use  = self.wrap.get()        
        if self.use:
            self.text.config(wrap=WORD)
            self.scrollbar_2.pack_forget()
            self.wrap.set(0)            
            
        else:
            self.text.config(wrap=NONE)
            self.scrollbar_2.pack(fill=X, expand=NO)
            self.wrap.set(1)
Reply
#5
(Mar-07-2019, 06:10 PM)Vicolas Wrote:
(Mar-03-2019, 11:13 PM)joe_momma Wrote: You're really close- I made some changes to your callback:
 def wrap_text(self): self.use = self.wrap.get() if self.use: self.text.config(wrap=WORD) self.wrap.set(0) else: self.text.config(wrap=NONE) self.wrap.set(1) 
Thank you very much for your reply. But is there a way I can also toggle (show/display) with tkinter x-scrollbar (Horizontal) on click of the 'Word wrap' submenu. Thanks again.
Thanks Sir.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,949 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020