Tying a GUI to a another module 
Author Message
 Tying a GUI to a another module

Does anyone know how to link a module containing the gui (first_gui)
and a another one containing the code to run the program
(fibonaccii_gui). Using the "Fib Calc" button to execute the process.
I want the program to take input from the Entry field process it
through "fibonaccii_gui" and then print the fibonaccii sequence in the
Label field. Here's the involved modules, Thank you for any help.

"first_gui" module
import fibonaccii_gui
from Tkinter import *
class fibon_gui:    
     def __init__(self, master):
          frame_entry = Frame(master)
          frame_entry.pack()
          input = Entry(frame_entry, width=30)
          input.pack()
          output = Label(frame_entry, text="Output goes here")
          output.pack(side=TOP)

          frame_button = Frame(master)
          frame_button.pack()
          button_fibcalc = Button(frame_button, text="Fib Calc")
          button_fibcalc.pack(side=LEFT)
          button_quit = Button(frame_button, text="QUIT", fg="red",
command=master.quit)
          button_quit.pack(side=RIGHT)
root = Tk()
root.title('Fibonaccii')
fibgui = fibon_gui(root)
root.mainloop()

Here's "fibonaccii_gui" module
class fibonaccii:
    def fib(self):
        a, b = 0, 1
        while b < fibnumber:
            #print b, fibonaccii sequence is saved in variable "b"
            a, b = b, a+b
fibinstance = fibonaccii()



Thu, 25 Dec 2003 00:56:31 GMT  
 Tying a GUI to a another module

Quote:

> Does anyone know how to link a module containing the gui (first_gui)
> and a another one containing the code to run the program
> (fibonaccii_gui). Using the "Fib Calc" button to execute the process.
> I want the program to take input from the Entry field process it
> through "fibonaccii_gui" and then print the fibonaccii sequence in the
> Label field. Here's the involved modules, Thank you for any help.

Rearranging the rest of your message to put the calculation module
first:

Quote:
> Here's "fibonaccii_gui" module
> class fibonaccii:
>     def fib(self):
>         a, b = 0, 1
>         while b < fibnumber:
>             #print b, fibonaccii sequence is saved in variable "b"
>             a, b = b, a+b
> fibinstance = fibonaccii()

A few points:
1) This module isn't doing anything gui-like.  You might as
   well just name it "fibonacci".
2) The "fib" function is always the same.  There's no point in
   making a whole class and an instance of the class just to
   hold it.  It can just as well be a top-level function in the
   module.
3) The comment suggests you want the sequence to be saved in b,
   but it won't be.  You should specifically make a list to hold
   the result of the calculation.  The "fib" function should
   return that list as its result, and leave it to the calling
   function to decide what it wants to do with the list (print it
   to the standard output, paint it on a GUI label, whatever).

Here's a new fibonacci module:

def fib(fibnumber):
    a, b = 0, 1
    result_list = [a, b]
    while b < fibnumber:
        a, b = b, a+b
        result_list.append(b)
    return result_list
# That's it.

Or, using python lists for all they're worth:

def fib(fibnumber):
    series = [0, 1]
    while series[-1] < fibnumber:
        series.append(series[-1] + series[-2])
    return series

Quote:

> "first_gui" module
> import fibonaccii_gui
> from Tkinter import *
> class fibon_gui:
>      def __init__(self, master):
>           frame_entry = Frame(master)
>           frame_entry.pack()
>           input = Entry(frame_entry, width=30)
>           input.pack()
>           output = Label(frame_entry, text="Output goes here")
>           output.pack(side=TOP)

>           frame_button = Frame(master)
>           frame_button.pack()
>           button_fibcalc = Button(frame_button, text="Fib Calc")
>           button_fibcalc.pack(side=LEFT)
>           button_quit = Button(frame_button, text="QUIT", fg="red",
> command=master.quit)
>           button_quit.pack(side=RIGHT)
> root = Tk()
> root.title('Fibonaccii')
> fibgui = fibon_gui(root)
> root.mainloop()

You're going to want to tie a function or method to the button
so that it'll know what to do when it's clicked.  It's easiest to
make this another method of the fibon_gui class.  First, you'll
need to stick a "self." in front of almost every variable name
in your __init__ method, otherwise all those variables will be
local to __init__ and none of the class's other methods will be able
to access the widgets.  Then write the method to be run when the
button is clicked.  Something like this.  (This is off the top
of my head and untested.  I can't guarantee it will work, though
it shouldn't fry your hard drive. :-))

    def do_calculation(self, event=None):
        number = self.input.get()
        number = int(number) # since the entry field holds a string
        # Have the other module calculate the list
        fiblist = fibonacci.fib(number)
        # Convert the list to a string representation and make
        # it the label's text property
        output['text'] = str(fiblist)

Now add another argument to your Button constructor in __init__,
setting this new method to be the button's command property:

        self.button_fibcalc = Button(frame_button, text="Fib Calc",
                                     command=self.do_calculation)

You should be almost done.  Good luck with the rest.

-- Kevin



Fri, 26 Dec 2003 02:02:33 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. ** APS (GUI) opportunity ** APS (GUI) ** APS (GUI) Experience required

2. Why TkInter? (Merits of GUI modules)

3. Python Module for Qt GUI Lib??

4. native Python GUI module(s)?

5. "Wpy" portable Python GUI module

6. Tk GUI + C++ modules ??

7. Brief description of TIE

8. TIE is the name

9. Tying Files in DOS

10. Question of J (Concerning TIE)

11. Question of J (Concerning TIE)

12. The ties that bind...

 

 
Powered by phpBB® Forum Software