Python GUI class calling function from separate file

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2009
Posts: 11
Reputation: romes87 is an unknown quantity at this point 
Solved Threads: 0
romes87 romes87 is offline Offline
Newbie Poster

Python GUI class calling function from separate file

 
0
  #1
Aug 12th, 2009
Hi,
I am new in Daniweb. I am trying to do a simple task in Python but cannot figure out how it works. Keeps getting it wrong.

Basically, I have a main GUI class App which has 2 entry boxes where you fill two numbers, a listbox where the results are produced.

a button calls a function ( def calc(self): ) which gets these two numbers and performs simple calculations like addition multiplication etc and prints them in the listbox.

I want this simple function calc(self) to be in a separate file and still be able to print the result in the GUI listbox.

Can anyone please help me with this.

here is the sample code:
  1. from Tkinter import *
  2.  
  3. class App:
  4.  
  5. def __init__(self, parent):
  6.  
  7. frame = Frame(parent.title("primary window"))
  8. frame.pack()
  9.  
  10. self.lbfirst = Label(frame, text="First number:")
  11. self.lbfirst.grid(row=0,column=0)
  12.  
  13. self.first = Entry(frame)
  14. self.first.grid(row=1,column=0)
  15.  
  16. self.lbsecond = Label(frame, text="Second number:")
  17. self.lbsecond.grid(row=2,column=0)
  18.  
  19. self.second = Entry(frame)
  20. self.second.grid(row=3,column=0)
  21.  
  22. self.number=Button(frame, text="calc", fg="red", command=self.calc)
  23. self.number.grid(row=4,column=0)
  24.  
  25. self.result = Listbox(frame)
  26. self.result.grid(row=5,column=0, columnspan=5, sticky=N+S+E+W)
  27.  
  28. def calc(self):
  29.  
  30. one = float(self.first.get())
  31. two = float(self.second.get())
  32.  
  33. def addition():
  34. add = one + 1
  35. print(add)
  36.  
  37. self.result.insert(END, 'first='+str(one)+' second='+str(two)+' addition='
  38. +str(add))
  39.  
  40. def subtraction():
  41. subs = two - 1
  42. print(subs)
  43.  
  44. self.result.insert(END, 'first='+str(one)+' second='+str(two)+' subtraction='
  45. +str(subs))
  46.  
  47.  
  48. self.result.insert(END, 'first='+str(one))
  49.  
  50. addition()
  51.  
  52. self.result.insert(END, 'second='+str(two))
  53.  
  54. subtraction()
  55.  
  56. root = Tk()
  57.  
  58. app = App(root)
  59.  
  60. root.mainloop()
Editor's note:
Please use code tags with your code to preserve the indentations. Otherwise the code is very difficult to read and not too many folks will help.

[code=python]
your Python code here
[/code]
Last edited by vegaseat; Aug 12th, 2009 at 12:08 pm. Reason: code tags added
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 71
Reputation: kolosick.m188 is an unknown quantity at this point 
Solved Threads: 15
kolosick.m188's Avatar
kolosick.m188 kolosick.m188 is offline Offline
Junior Poster in Training

Re: Python GUI class calling function from separate file

 
0
  #2
Aug 12th, 2009
Create a new file called calc.py. Put the calc function in it and then in your main file import calc.py
There are 10 types of people in the world, those who get this, those who don't, and those who thought this was binary.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 11
Reputation: romes87 is an unknown quantity at this point 
Solved Threads: 0
romes87 romes87 is offline Offline
Newbie Poster

Re: Python GUI class calling function from separate file

 
0
  #3
Aug 12th, 2009
Thx for your reply kolosick.m188.

I have tried that. It does the calculation but it fails to print the result in the listbox back in the main GUI.

when i pass the variables "one" and "two" from main file to the calc.py function, it receives it ok and does add, subtract etc (i have used the print command to check)
But it fails when I want to go back in the main file and put the result in the listbox.

do you have any idea about this?
Last edited by romes87; Aug 12th, 2009 at 12:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,071
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: Python GUI class calling function from separate file

 
0
  #4
Aug 12th, 2009
There is no difference, as far a calling a function goes, between calling a function within or without a program. The following works for me with the calc() function contained in the same program file.
  1. from Tkinter import *
  2.  
  3. def calc(one, two, result):
  4.  
  5. def addition():
  6. add = one + 1
  7. print(add)
  8.  
  9. result.insert(END, 'first='+str(one)+' second='+str(two)+' addition='
  10. +str(add))
  11.  
  12. def subtraction():
  13. subs = two - 1
  14. print(subs)
  15.  
  16. result.insert(END, 'first='+str(one)+' second='+str(two)+' subtraction='
  17. +str(subs))
  18.  
  19.  
  20. result.insert(END, 'first='+str(one))
  21.  
  22. addition()
  23.  
  24. result.insert(END, 'second='+str(two))
  25.  
  26. subtraction()
  27.  
  28.  
  29. class App:
  30.  
  31. def __init__(self, parent):
  32.  
  33. frame = Frame(parent.title("primary window"))
  34. frame.pack()
  35.  
  36. self.lbfirst = Label(frame, text="First number:")
  37. self.lbfirst.grid(row=0,column=0)
  38.  
  39. self.first = Entry(frame)
  40. self.first.grid(row=1,column=0)
  41.  
  42. self.lbsecond = Label(frame, text="Second number:")
  43. self.lbsecond.grid(row=2,column=0)
  44.  
  45. self.second = Entry(frame)
  46. self.second.grid(row=3,column=0)
  47.  
  48. self.number=Button(frame, text="calc", fg="red", \
  49. command=self.call_calc)
  50. self.number.grid(row=4,column=0)
  51.  
  52. self.result = Listbox(frame)
  53. self.result.grid(row=5,column=0, columnspan=5, sticky=N+S+E+W)
  54.  
  55. def call_calc(self):
  56. calc(float(self.first.get()), float(self.second.get()), \
  57. self.result)
  58.  
  59.  
  60. root = Tk()
  61.  
  62. app = App(root)
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 11
Reputation: romes87 is an unknown quantity at this point 
Solved Threads: 0
romes87 romes87 is offline Offline
Newbie Poster

Re: Python GUI class calling function from separate file

 
0
  #5
Aug 13th, 2009
Thanks wooee!! a few changes here and there and it does the job! do you know why if i put the function in a separate file, it does not recognise END term in the
result.insert(END, 'first=', str(one)...)

it prompts the error : NameError: global name END not defined
thanks again!
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 11
Reputation: romes87 is an unknown quantity at this point 
Solved Threads: 0
romes87 romes87 is offline Offline
Newbie Poster

Re: Python GUI class calling function from separate file

 
0
  #6
Aug 13th, 2009
oh don't worry about my previous post. I just forgot to import tkinter in that separate file.

thanks again!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 339 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC