1

I'm trying to do a GUI to do a plot. The idea is that when I click "Submit" the plot will pop up, if I then click "Submit" again, the plot should close and open again.

However, When I click "Submit" the plot shows up correctly, but I need to close the plot window manually for the button to release again?

I've cleaned up my code to only contain the essentials:

import matplotlib.pyplot as plt  # import plot functions
from mpl_toolkits.mplot3d import Axes3D
from Tkinter import *

################################ TKINTER GUI ##################################

root = Tk()

label_1 = Label(root, text="File name:")
entry_1 = Entry(root)
entry_1.insert(0,"Input")
label_1.grid(sticky=E)
entry_1.grid(row=0, column=1, columnspan=3)

def saveentry():
    plt.close()
    name1 = entry_1.get()

################################### PLOTTING ##################################

    fig = plt.figure()
    ax = fig.gca(projection='3d') 

    plt.show(fig)

Button_1 = Button(root, text="Submit", command=saveentry)
Button_1.grid(row=7,column=0, sticky=E)

root.mainloop()

# END OF SCRIPT

What am I doing wrong?

Martin
  • 353
  • 1
  • 6
  • 23

1 Answers1

2

You need to switch the backend from Qt4 to Tk. What you're currently doing is you open a Qt based plot window from a Tkinter based application. This work but isn't interoperable.

Insert the following line somewhere at the top (not in the saveentryfunction):

plt.switch_backend('TkAgg')  # TkAgg (instead Qt4Agg)
Günther Eberl
  • 674
  • 1
  • 10
  • 19
  • It works like an absolute charm when I run the Python script, however, when I export the script into an exe file the same problem persists. It's as if the line with the backend switch is not recognized? – Martin Apr 13 '18 at 10:43
  • @Martin What do you freeze your Python script with? cxfreeze? Py2Exe? I guess you need to add all the Qt library stuff to the exclude list. This should also make your exe smaller. – Günther Eberl Apr 13 '18 at 10:47
  • @Günter sorry, I'm brand new to Python, but I assume that "freeze" means the py to exe converter? In that case, I'm using "Auto Py to Exe". – Martin Apr 13 '18 at 11:14
  • @Martin Yes, "freezing" is the act of packaging Python scripts and dependencies into a executable that doesn't need the Python runtime installed. Your GUI program uses [PyInstaller](http://www.pyinstaller.org/index.html) under the hood, it effectively just constructs a lengthy command line (see "advanced" button). I'd ditch it completely to be honest and just work with the command line. Then you can refer to https://stackoverflow.com/a/17595149/8137043 to edit the packaged libraries. – Günther Eberl Apr 13 '18 at 11:41
  • @ Günther I've tried just using the pyinstaller command line and manipulated the .spec file to exclude the PyQt4 package. However, now the program won't run at all? I'm not really sure how to get an overview of what to exclude and what not to exclude? – Martin Apr 15 '18 at 10:48
  • @Martin Please open a new question, we're too far off track here ;) – Günther Eberl Apr 15 '18 at 17:24
  • @ Günther fair enough. Thanks for the help! – Martin Apr 16 '18 at 07:57