943,676 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1464
  • Python RSS
Aug 12th, 2009
0

Python GUI class calling function from separate file

Expand Post »
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:
python Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
romes87 is offline Offline
18 posts
since Aug 2009
Aug 12th, 2009
0

Re: Python GUI class calling function from separate file

Create a new file called calc.py. Put the calc function in it and then in your main file import calc.py
Reputation Points: 16
Solved Threads: 16
Junior Poster in Training
kolosick.m188 is offline Offline
72 posts
since Jun 2009
Aug 12th, 2009
0

Re: Python GUI class calling function from separate file

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
romes87 is offline Offline
18 posts
since Aug 2009
Aug 12th, 2009
0

Re: Python GUI class calling function from separate file

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.
Python Syntax (Toggle Plain Text)
  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)
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,302 posts
since Dec 2006
Aug 13th, 2009
0

Re: Python GUI class calling function from separate file

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
romes87 is offline Offline
18 posts
since Aug 2009
Aug 13th, 2009
0

Re: Python GUI class calling function from separate file

oh don't worry about my previous post. I just forgot to import tkinter in that separate file.

thanks again!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
romes87 is offline Offline
18 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Find and seperate around substring
Next Thread in Python Forum Timeline: accessing dictionary values to work with module





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC