Tying a GUI to a another module
Kevin Russell
prairiesasquatch at crosswinds.net
Sun Jul 8 14:02:33 EDT 2001
eric_brake wrote:
>
> 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:
> 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
>
> "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
More information about the Python-list
mailing list