Python Forum
[Tkinter] Class with text widget method
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Class with text widget method
#2
1. text_box need to be part of the class. don't make a new one each time method is called.
put in __init__
self.text_box = Text(self.root, bg = 'blue', fg = 'white')
self.text_box.pack(side=LEFT)
here another example
import tkinter as tk

# just inherit it to make custom tk.Text
class Custom_Console(tk.Text):
    def __init__(self, master):
        tk.Text.__init__(self, master, bg = 'blue', fg = 'white')
        self.pack(side = tk.LEFT)

    #custom methods
    def write(self, content):
        self.insert(tk.END, content)

    def writeln(self, content):
        self.insert(tk.END, content + '\n')

def main():
    root = tk.Tk()
    app = Custom_Console(root)
    app.insert(tk.END, "0 ")
    app.write("1 ")
    app.writeln("2")
    app.writeln("Here we go")
    app.mainloop()

if __name__ == '__main__':
    main()
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
RE: Class with text widget method - by Windspar - Nov-14-2017, 11:09 PM
RE: Class with text widget method - by Larz60+ - Nov-14-2017, 11:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TKinter Widget Attribute and Method Quick Reference zunebuggy 3 1,017 Oct-15-2023, 05:49 PM
Last Post: zunebuggy
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 5,398 Jun-26-2022, 06:26 PM
Last Post: menator01
  [Tkinter] Text widget inert mode on and off rfresh737 5 4,079 Apr-19-2021, 02:18 PM
Last Post: joe_momma
  Line numbers in Text widget rfresh737 3 5,658 Apr-15-2021, 12:30 PM
Last Post: rfresh737
  tkinter text widget word wrap position chrisdb 6 7,788 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  method to add entries in multi columns entry frames in self widget sudeshna24 2 2,403 Feb-19-2021, 05:24 PM
Last Post: BashBedlam
  [PyQt] remove widget from other class issac_n 2 3,259 Aug-05-2020, 01:55 PM
Last Post: deanhystad
  [Tkinter] Get the last entry in my text widget Pedroski55 3 6,627 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  How to place global tk text widget in class or on canvas puje 1 2,418 Jul-04-2020, 09:25 AM
Last Post: deanhystad
  [PyQt] I get a name Name Error: when calling a method inside a class radoo 2 2,481 Jun-11-2020, 05:02 PM
Last Post: radoo

Forum Jump:

User Panel Messages

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