storing output text in a tkinter textbox

Question:

im looking to output a list of text to both a text file as well as a tkinter textbox. Currently I have it working to print to a text file being held in the same folder. I would like this same list to be printed on screen into the outputToScreen textbox

From my research I think I am meant to use .insert(index , text) but I can not get this method to work. I have also seen some old code using a .set function but when I tried this I came across an error saying that textbox’s don’t have that method

#file -> gui.py
# IMPORT tkinter for GUI creation and import scripts for functions
from tkinter import *
from tkinter.ttk import *
import scripts
import customtkinter
windowDim = str(750)+'x'+str(600)
window = customtkinter.CTk()

window.title("INFO GENERATOR")
window.geometry(windowDim)
window.resizable(True, True)
# Modes: system (default), light, dark
customtkinter.set_appearance_mode("dark")
# Themes: blue (default), dark-blue, green
customtkinter.set_default_color_theme("customTkinterTheme.json")

outputToScreen = customtkinter.CTkTextbox(window , width=400 , height=200)
outputToScreen.place(x=200, y=300)

def popupmsg(msg):
    popup = customtkinter.CTk()
    popup.title("!")
    label = customtkinter.CTkLabel(popup, text=msg,)
    label.pack(side="top", fill="x", pady=10)
    B1 = customtkinter.CTkButton(popup, text="Okay", command=popup.destroy)
    B1.pack()
    popup.mainloop()


# create main window
# window specs are 700 x 350 and window is not able to be resizeable

# CREATING USER CONTROLS
# each button will print a list of items (number of items will be come from the entry userValue)
userEntry = customtkinter.CTkEntry(window)
# userValue = int(userValue)

userValue = ""

userEntry.place(x=325, y=200)
userEntry.insert(0, userValue)


userValueLabel = customtkinter.CTkLabel(
    window, text="ENTER AMOUNT OF RECORDS YOU WOULD LIKE").place(x=275, y=170)
label = customtkinter.CTkLabel(window, text="INFOMATION GENERATOR by Noah Mackay "
                               ).pack()


def outputEmails(userValue):
    # function receives the amount of records the user wants to print
    # function will be called when email button is clicked
    # this function will clear the output file and then read open it
    # will call the generateEmail function from scripts.py file
    # outputs the amount of records based on the user input in the entry text box
    userValue = int(userEntry.get())
    outputFile = scripts.generateEmail(userValue)
    file = open('output.txt', 'w').close()
    file = open('output.txt', 'w')
    file.write(str(outputFile))
    outputToScreen.set(outputFile)
    popupmsg("PRINTING WORKED")
   
    

def outputNames(userValue):
    # function receives the amount of records the user wants to print
    # function will be called when email button is clicked
    # this function will clear the output file and then read open it
    # will call the generateEmail function from scripts.py file
    # outputs the amount of records based on the user input in the entry text box
    userValue = int(userEntry.get())
    outputFile = scripts.generateName(userValue)
    file = open('output.txt', 'w').close()
    file = open('output.txt', 'w')
    file.write(str(outputFile))
    outputToScreen.set(outputFile)
    popupmsg("PRINTING COMPLETED")
   

def outputCost(userValue):
    userValue = int(userEntry.get())
    outputFile = scripts.generateCost(userValue)
    file = open('output.txt', 'w').close()
    file = open('output.txt', 'w')
    file.write(str(outputFile))
    outputToScreen.set(outputFile)
    popupmsg("PRINTING COMPLETED")
   

def outputProduct(userValue):
    userValue = int(userEntry.get())
    outputFile = scripts.generateProduct(userValue)
    file = open('output.txt', 'w').close()
    file = open('output.txt', 'w')
    file.write(str(outputFile))
    outputToScreen.set(outputFile)
    popupmsg("PRINTING COMPLETED")


def outputPhoneNumber(userValue):
    userValue = int(userEntry.get())
    outputFile = scripts.generatePhoneNumber(userValue)
    file = open('output.txt', 'w').close()
    file = open('output.txt', 'w')
    file.write(str(outputFile))
    outputToScreen.insert(0,outputFile)
    popupmsg("PRINTING COMPLETED")
    
# creates 5 buttons each have their respective output function attached using command=


emailButton = customtkinter.CTkButton(window, text="EMAILS",
                                      command=lambda: outputEmails(userValue)).place(x=5, y=40)


productButton = customtkinter.CTkButton(window, text="PRODUCTS",
                                        command=lambda: outputProduct(userValue)).place(x=150, y=40)
phoneNumberButton = customtkinter.CTkButton(
    window, text="PHONE NUMBERS" , command= lambda:outputPhoneNumber(userValue)).place(x=300, y=40)
costButton = customtkinter.CTkButton(window, text="PRICES",
                                     command=lambda: outputCost(userValue)).place(x=450, y=40)
nameButton = customtkinter.CTkButton(
    window, text="FIRST + LAST NAMES", command=lambda: outputNames(userValue)).place(x=600, y=40)



window.mainloop()
#file -> scripts.py
import random


def generateName(numberOfItemsNeed):
    # opens 2 files. One containing first names and the other containing last names
    firstNameFile = open('dataFilesFirstNamesData.txt', 'r')
    lastNameFile = open('dataFilesLastNamesData.txt', 'r')
    # builds values for the while statement and the return string
    returnValue = ""
    counter = 0

# builds the return string using a while loop and removing the newline character
# after each line from both files when being read
    while counter < int(numberOfItemsNeed):
        returnValue += str(firstNameFile.readline().strip("n")
                           ) + " " + str(lastNameFile.readline().strip("n")) + "n"
        counter = counter + 1
# returns a list of "human" names in a single string divided by a newline character
    return (returnValue)


def generateEmail(numberOfItemsNeed):
    # opens a file containing a records of first names
    firstNameFile = open('dataFilesdictonary.txt', 'r')
    counter = 0
    # A list of commonly used email address suffixs
    suffix = ['@gmail.com', '@gmail.ca', '@hotmail.com',
              '@hotmail.ca', '@mail.com ', '@mail.ca', '@gov.ca']
    returnValue = ""

    while counter < int(numberOfItemsNeed):
        returnValue += firstNameFile.readline().strip("n") + 
            str((random.randrange(0, 100))) + 
            suffix[random.randrange(0, len(suffix))]+'n'
        counter = counter + 1
    return (returnValue)


def generateCost(numberOfItemsNeed):
    # generates a random item price in the inclusive range of 0.00$ to 1000.99$
    counter = 0
    cost = ""
    while counter < int(numberOfItemsNeed):
        cost += '$' + str(random.randrange(0, 1000)) + 
            "." + str(random.randrange(0, 99)) + 'n'
        counter = counter+1
    return cost


def generateProduct(numberOfItemsNeed):
    counter = 0
    returnValue = ""
    productList = open('dataFilesitemData.txt', 'r')
    while counter < int(numberOfItemsNeed):
        returnValue += str(productList.readline()
                           ).strip("n") + str(generateCost(1))
        counter = counter + 1
    return (returnValue)


def generatePhoneNumber(numberOfItemsNeed):
    counter = 0
    returnValue = ""
    while counter < int(numberOfItemsNeed):

        firstNumber = str(random.randrange(100, 999))
        secondNumber = str(random.randrange(1000, 9999))
        thirdNumber = str(random.randrange(100, 999))

        returnValue += firstNumber + "-" + secondNumber + "-" + thirdNumber + 'n'
        counter = counter + 1
    return (returnValue)


def shuffleFile(filePath):
    lines = open(filePath).readlines()
    random.shuffle(lines)
    open(filePath, 'w').writelines(lines)
        
# shuffleFile('dataFilesdictonary.txt')
Asked By: npmackay

||

Answers:

Text widget indexes are strings of the form line.character with lines starting at 0 and characters starting at 1. So, to insert at the start you need to use the index "1.0", not 0. If you want to insert at the end you can use the special index "end". If the widget is empty, "1.0" and "end" yield identical results.

outputToScreen.insert("1.0", outputToFile)
Answered By: Bryan Oakley
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.