0

I wrote a script for the Collatz Conjecture within a Tkinter GUI and displaying a Matplotlib graph of the result of the algorithm. Everything works OK except, the graph displays perfectly but then stops the script without populating the data field which displays the data points on which the graph is based. Only when I close the graph does is the data field populated.

I'm sure I'm doing something wrong in the script (probably indents) but I've tried everything but can't make it work. The script is added below:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from tkinter import *
from tkinter import messagebox
root=Tk()
root.title("Collatz  Conjecture")

import matplotlib.pyplot as plt
import textwrap
from IPython import get_ipython

# sget_ipython().run_line_magic('matplotlib', 'qt')
#import matplotlib.backends.tkagg as tkagg
#from matplotlib.backends.backend_agg import FigureCanvasAgg


 # Functions

lst = []

def collatz(num):
    lst.clear()
    while num != 1:
        lst.append(num)

        if num % 2 == 0:
            num = int(num / 2)

        else:
            num = int(3 * num + 1)

def main(event):
    num = int(input.get())

    collatz(num)



    plt.plot(lst)
    plt.show()

    output1.delete(1.0, END)
    output1.insert(END, lst)
    output2.delete(1.0, END)
    output2.insert(END, "Number of iterations: " + str(len(lst)))

lbl1 = Label(root, width = 20, text = "Type in number\n & press Enter")
lbl1.grid(row = 1, column = 0, sticky = W)
lbl2 = Label(root, width = 40, text = "THE COLLATZ CONJECTURE")
lbl2.grid(row = 4, column = 0)

input = Entry(root, width = 20, bg = "light grey")
input.grid(row = 1, padx = 6, sticky = E)
input.get()
input.bind("<Return>", main)

canv = Canvas(root, width= 350, height= 350, bg = "white")
canv.grid(row = 6, column = 0, padx = (5,5), pady = (5,5))

img = PhotoImage(file = "/Users/andrehuman/Desktop/collatz_conjecture.png") # Tip to get full path: pull file into terminal!
canv.create_image(25, 25, image = img, anchor = NW)

bt1 = Button(root, width = 10, text = "About")
bt1.grid(row = 7, column = 0, pady = (5,7))

output1 = Text(root, wrap = WORD, width = 50, height = 7, bg = "light grey")  # Note word wrap attribute
output1.grid(row = 3, column = 0, padx = (5,1), sticky = W)
output2 = Text(root, width = 50, height = 1, bg = "white")
output2.grid(row = 2, column = 0, sticky = W)


def about():

    messagebox.showinfo("About", "The Collatz conjecture states that if you pick any positive whole number, and if its even, you divide it by two and if its odd, you multiply it by three and add one, and if you repeat this procedure often enough, the number that you started with will eventually reduce to one and if you play this game for long enough, your friends will eventually stop calling to see if you want to hang out ")

btn1 = Button(root, text = "About", command = about)
btn1.grid(row = 7, column = 0, pady = (5,7))


root.mainloop()

Any help would be appreciated.

Andre

PS, I'm not a professional coder or student coder as such... I'm doing this as a hobby and to inform myself about coding.

Andre Human
  • 25
  • 1
  • 2
  • 1
    The main problem you have is that you use `pyplot` within your custom GUI. That will most often conflict and result in problems you cannot easily track down without a lot of experience. In your case, you might get away with calling `plt.gcf().show()` instead of `plt.show()` but I cannot test (no [mcve] given), and in general you might consider creating your own Tk window with the figure to show inside of it, such as to avoid the use of pyplot (note that the official matplotlib examples on embedding do not use pyplot!) – ImportanceOfBeingErnest Jan 28 '19 at 16:01
  • In addition, please have a look at [this](https://stackoverflow.com/q/28269157/2454357). – Thomas Kühn Jan 28 '19 at 16:03
  • Thank you for the answers plt.gcf() and plt.ion() before plt.show() both work – Andre Human Jan 28 '19 at 16:43

0 Answers0