Output a function in tkinter that prints
Question: I have created a function, which takes two arguments, prints multiple
statements out and eventually returns an answer. It works great in the python
shell.

I am using tkinter (python 3.4.1) to create a user friendly program for
consumers to use my function. I wish to have my function output everything to
something (I am using a listbox) and then the user could scroll through the
statements. However, it only outputs the return value, and not the print
statements. Why??

This is my code, and I believe the problem is in the PageOne class.

import tkinter as tk from tkinter import ttk from Collections import primes
from Collections import isprime from Collections import LS import sys

    
    
    LARGE_FONT = ("Verdana", 12)
    NORM_FONT = ("Verdana", 10)
    SMALL_FONT = ("Verdana", 8)
    
    def popupmsg(msg):
        popup = tk.Tk()
    
    popup.wm_title("!")
    label = ttk.Label(popup, text=msg, font=NORM_FONT)
    label.pack(side="top", fill="x", pady = 10)
    B1 = ttk.Button(popup, text="Okay", command = popup.destroy)
    B1.pack()
    popup.mainloop()
    
    
    class Mathapp(tk.Tk):
    
    def __init__(self, *args, **kwargs):
    
        tk.Tk.__init__(self, *args, **kwargs)
    
        tk.Tk.iconbitmap(self, default="psi.ico")
        tk.Tk.wm_title(self, "Math App")
    
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
    
        menubar = tk.Menu(container)
        filemenu = tk.Menu(menubar, tearoff = 0)
        filemenu.add_command(label = "Save settings", command = lambda: popupmsg("Not supported just yet!"))
        filemenu.add_separator()
        filemenu.add_command(label = "Exit", command = quit)
        menubar.add_cascade(label = "File", menu=filemenu)
    
        prog = tk.Menu(menubar, tearoff = 0)
        prog.add_command(label = "Legendre Symbol", command = lambda: popupmsg("Not supported just yet!"))
        prog.add_command(label = "Prime Sieve", command = lambda: popupmsg("Not supported just yet!"))
        prog.add_command(label = "Prime Factorisation - Sieve", command = lambda: popupmsg("Not supported just yet!"))
        menubar.add_cascade(label = "Programs", menu=prog)
    
        tk.Tk.config(self, menu=menubar)
    
        self.frames = {}
    
        for F in (StartPage, PageOne, PageTwo, PageThree):
    
            frame = F(container, self)
    
            self.frames[F] = frame
    
            frame.grid(row=0, column=0, sticky="nsew")
    
            self.show_frame(StartPage)
    
    def show_frame(self, cont):
    
        frame = self.frames[cont]
        frame.tkraise()
    
    class StartPage(tk.Frame):
    
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Start Page", font=LARGE_FONT)
        label.pack(pady = 10, padx = 10)
    
        button1 = ttk.Button(self, text="Legendre Symbol",
                            command = lambda: controller.show_frame(PageOne))
        button1.pack()
    
        button2 = ttk.Button(self, text="Prime test - Sieve",
                            command = lambda: controller.show_frame(PageTwo))
        button2.pack()
    
        button3 = ttk.Button(self, text="Prime Factorisation",
                            command = lambda: controller.show_frame(PageThree))
        button3.pack()
    
    
    
    class PageOne(tk.Frame):
    
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Legendre Symbol", font=LARGE_FONT)
        label.pack(pady = 10, padx = 10)
    
        button1 = ttk.Button(self, text="Back to home",
                            command = lambda: controller.show_frame(StartPage))
        button1.pack()
    
        ##################
    
        def show_answer():
            Ans = str(LS(int(num1.get()),int(num2.get())))
            listbox.delete(0,"end")
            listbox.insert(0, LS(int(num1.get()),int(num2.get())))
    
    
        tk.Label(self, text = "Enter the numbers to be tested ").pack()
        num1 = tk.Entry(self)
        num1.pack()
        num2 = tk.Entry(self)
        num2.pack()
        blank = tk.Scrollbar(self)
        blank.pack()
    
        listbox= tk.Listbox(self, yscrollcommand=blank.set)
        listbox.pack()
    
        tk.Button(self, text = "Test", command = show_answer).pack()
    
        ##################
    
    app = Mathapp()
    app.mainloop()
    


Answer: It looks like the problem is probably here:

    
    
    def show_answer():
        Ans = str(LS(int(num1.get()),int(num2.get())))
        listbox.delete(0,"end")
        listbox.insert(0, LS(int(num1.get()),int(num2.get())))
    

I'd suggest creating a couple of entry widgets from the command line, and
modifying this function until you can get and print the contents of the
entries. You may have too much packed into one line in the `Ans` and
`listbox.insert` lines; try separating the different steps into different
lines of code. When you've got the entry widget contents, you might try
inserting them one at a time at the `end` index of the listbox (which of
course will be `0` if there's nothing in it yet, but `1` if there's already an
item).

