0

I am having problem with having a GUI interface to be implemented via a second file which just contains the file to read, plots made and some new functions to be evaluated based on that.

I am trying to create a GUI application using Tkinter. The way I am doing is as follows. I have a background script (say Background.py) which has two functions. Function X loads a data file, does some calculations and outputs a graph. The way I want to trigger this is via a GUI script in another file (GUI.py) which opens a panel with a button and when I click the button the function X in file Background.py should be evaluated and a plot should be shown. Once I check the plot, I can hit another button to close the plot and terminate the function X. Now I can choose to click another button to trigger the function Y in the file Background.py. These button should allow me to input three values, which should be the input to the function Y in the file Background.py. Once I hit this button, it should trigger the function Y and do what it it asks it to do. Now at the end, after that I can hit the button to close the gui.

How can I do this?. A general rough idea will be helpful.

I have put an example as much as I can:( at least the skeleton of the code) I have a background file say (Background.py) and gui file ( say GUI.py)

Background.py

import numpy

import matplotlib.pyplot as plt

import pandas

def progX():
     df = pd.read (myfile)

     ##df.stats # doing something and generating a plot from the file

    plt.boxplot(df['col'])

    plt.show()



def progY(y1, y2,y3):

    ## get the y1, y2, y3 from the GUI interface which the user has entered 

    #run a code...  and generate an output file

GUI.py

import Background as bg   
from tkinter import *
from tkinter.ttk import *

class GUI ():

    def create widgets(self):
        #....

    def create_panel2(self):
        #create buttons
        panel1 = ...
        btn1 = Button(panel1, text="yyyyy", command=bg.progA)
        btn1.pack() 

    def create_panel2(self):
        #create buttons
        panel2 = ...
        btn2 = Button(panel1, text="yyyyy", command=bg.progB)
        btn2.pack() 

All_Entries = []

window = Tk()
D=GUI(window)
window.mainloop()
runprogram1 = bg.progX()
runprogram2 = bg.probY(x, y, z)

My question is now, does the above makes sense? How can I call the background functions from the GUI? The statements runprogram1 & runprogram2 are definitely not correct, How can I implement that. Also how will I ensure that I call the proram Y in Background once I have close the output from the program X?

I guess the questions makes sense. I am new to GUI and having hard time working this out, which I need to. any help will be very much appreciated.

5
  • Is your Background.py correct? I think you are missing ' on plt.boxplot(df['col]). Commented Oct 19, 2018 at 17:44
  • Yes Black Thunder you are correct. I updated that. But that was not the issue. My problems are much deeper. Thanks. Commented Oct 19, 2018 at 18:23
  • Can anybody answer this. Even some basic help may be beneficial.. Commented Oct 19, 2018 at 19:53
  • Just to make sure are you aware that your code after the mainloop will never run until the GUI is closed. Commented Oct 19, 2018 at 20:28
  • Yes.. Nut I am wanting to ask if I am going in the righ direction Commented Oct 19, 2018 at 21:00

1 Answer 1

1

I'm assuming progA == progX, and progB == progY?

As your code is currently structured, some function in the GUI needs to get y1, y2 and y3 from a widget (Entry(), presumably), and pass to progY. progY can't fetch that info, b/c progY isn't aware of the widgets in the GUI. Get those values in the GUI class by binding the button to another function that 1) calls .get() on the Entry() widget, and then 2) passes those values to progY.

Make your Entry boxes in the GUI:

e1 = Entry(panel1)
e2 = Entry(panel1)
e3 = Entry(panel1)
self.entries = (e1, e2, e3)
for e in self.entries:
    e.pack()

Make a function that gets values and calls progY in the GUI:

def get_entries_call_y(self):
    e = [x.get() for x in self.entries]
    bd.progY(e[0], e[1], e[2])

Bind your button to get_entries_call_y (not to bd.progY):

btn2 = Button(panel1, text="yyyyy", command=get_entries_call_y)

If you'd like advice on structuring a GUI program in general, try adhering (as best you can) to a standard user interface architecture like model-view-controller (https://en.wikipedia.org/wiki/Model-view-controller). The solution I described above should make your program work, but the way you've structured your program isn't really a good way to do it for a lot of reasons. Learning MVC will help you organize what task should go into what function/class/file, give you a logical framework for adding features and improving your code, and allow you to create new GUI programs more efficiently.

Or at least that's what I got out of learning MVC.

Sign up to request clarification or add additional context in comments.

1 Comment

Wonderful.. Thanks a lot mRotten. I will go through it and restructure and keep you posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.