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
#1
I am quite new to Python and programming in general and just starting to work with Tkinter, so your patience and help is really appreciated.

I am trying to create a very simple Class that can display a string in a very rudimentary GUI by way of Tkinter and the 'text' widget. The goal is to be able to import this class, create an instance, and display text by calling a method that I built into this class.

I have been able to make it function, but not optimally. I have not been able to successfully create and pack the text widget with the constructor within my class ("__init__"). I have had to do it in actually within the method that I am using to insert content into the text widget.

This seems terribly inefficient. I would like to do all the set-up (frames, widgets, packing, etc) with the constructor and be able to reference them with functions.

Thanks in advance for any assistance.
Here is my script:

# Goal: create class and object/s to use in other apps

# Import module/s
from tkinter import *

# Create class
class Custom_Console:

	
	# Initialize root object
	def __init__(self, name):
	
		self.name = "new console"
		self.root = Tk()
		
		
		
	# Create widgets and call main loop in method (not optimal)
	def print_message(self, content):
	
		text_box = Text(self.root, bg="blue", fg="white")
		text_box.pack(side=LEFT)
		text_box.insert(END, content)
		self.root.mainloop()
	
		

# create object from 'Custom_Console' class
mycon = Custom_Console("main console")

# Declare variable for inserting text
content = "Here we go!!!"

# Call 'print_message' method to insert text into widget
mycon.print_message(content)
	
	
Reply


Messages In This Thread
Class with text widget method - by AeranicusCascadia - Nov-14-2017, 09:09 PM
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 914 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,091 Jun-26-2022, 06:26 PM
Last Post: menator01
  [Tkinter] Text widget inert mode on and off rfresh737 5 3,953 Apr-19-2021, 02:18 PM
Last Post: joe_momma
  Line numbers in Text widget rfresh737 3 5,521 Apr-15-2021, 12:30 PM
Last Post: rfresh737
  tkinter text widget word wrap position chrisdb 6 7,652 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  method to add entries in multi columns entry frames in self widget sudeshna24 2 2,312 Feb-19-2021, 05:24 PM
Last Post: BashBedlam
  [PyQt] remove widget from other class issac_n 2 3,190 Aug-05-2020, 01:55 PM
Last Post: deanhystad
  [Tkinter] Get the last entry in my text widget Pedroski55 3 6,495 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  How to place global tk text widget in class or on canvas puje 1 2,365 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,426 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