Nov-14-2017, 09:09 PM
(This post was last modified: Nov-14-2017, 09:09 PM by AeranicusCascadia.)
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:
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)