| | |
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:
Solved Threads: 0
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:
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]
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)
from Tkinter import * class App: def __init__(self, parent): frame = Frame(parent.title("primary window")) frame.pack() self.lbfirst = Label(frame, text="First number:") self.lbfirst.grid(row=0,column=0) self.first = Entry(frame) self.first.grid(row=1,column=0) self.lbsecond = Label(frame, text="Second number:") self.lbsecond.grid(row=2,column=0) self.second = Entry(frame) self.second.grid(row=3,column=0) self.number=Button(frame, text="calc", fg="red", command=self.calc) self.number.grid(row=4,column=0) self.result = Listbox(frame) self.result.grid(row=5,column=0, columnspan=5, sticky=N+S+E+W) def calc(self): one = float(self.first.get()) two = float(self.second.get()) def addition(): add = one + 1 print(add) self.result.insert(END, 'first='+str(one)+' second='+str(two)+' addition=' +str(add)) def subtraction(): subs = two - 1 print(subs) self.result.insert(END, 'first='+str(one)+' second='+str(two)+' subtraction=' +str(subs)) self.result.insert(END, 'first='+str(one)) addition() self.result.insert(END, 'second='+str(two)) subtraction() root = Tk() app = App(root) root.mainloop()
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
•
•
Join Date: Aug 2009
Posts: 11
Reputation:
Solved Threads: 0
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?
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.
•
•
Join Date: Dec 2006
Posts: 1,071
Reputation:
Solved Threads: 299
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)
from Tkinter import * def calc(one, two, result): def addition(): add = one + 1 print(add) result.insert(END, 'first='+str(one)+' second='+str(two)+' addition=' +str(add)) def subtraction(): subs = two - 1 print(subs) result.insert(END, 'first='+str(one)+' second='+str(two)+' subtraction=' +str(subs)) result.insert(END, 'first='+str(one)) addition() result.insert(END, 'second='+str(two)) subtraction() class App: def __init__(self, parent): frame = Frame(parent.title("primary window")) frame.pack() self.lbfirst = Label(frame, text="First number:") self.lbfirst.grid(row=0,column=0) self.first = Entry(frame) self.first.grid(row=1,column=0) self.lbsecond = Label(frame, text="Second number:") self.lbsecond.grid(row=2,column=0) self.second = Entry(frame) self.second.grid(row=3,column=0) self.number=Button(frame, text="calc", fg="red", \ command=self.call_calc) self.number.grid(row=4,column=0) self.result = Listbox(frame) self.result.grid(row=5,column=0, columnspan=5, sticky=N+S+E+W) def call_calc(self): calc(float(self.first.get()), float(self.second.get()), \ self.result) root = Tk() app = App(root)
Linux counter #99383
![]() |
Similar Threads
- Python GUI Programming (Python)
- calling a function in DLL file ??? (C++)
- Calling a function in html file from c# code (C#)
- exporting MatLab trained neural network to separate file/function (Legacy and Other Languages)
- How do I declare a class member function in another class? (Python)
- Python GUI Problem (Python)
- Calling c function from python (Python)
- Reading an input file as a class memeber function (C++)
Other Threads in the Python Forum
- Previous Thread: Find and seperate around substring
- Next Thread: accessing dictionary values to work with module
Views: 339 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Python
anti array avogadro beginner builtin clear client code color count csv curved def dictionary dynamic enter examples excel file float format frange ftp function gui heads hints homework import input java lapse line lines linux list lists loop microcontroller mouse multiple mysqldb mysqlquery newb number numbers output parsing path port prime program programming projects py2exe pygame pyopengl pyqt python random raw_input recursion recursive redirect script scrolledtext singleton software sqlite ssh stderr string strings subprocess sum syntax table terminal text thread threading time tkinter tlapse tooltip tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape wikipedia windows word wx.wizard wxpython






