LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2008, 03:24 AM   #1
raskol
Member
 
Registered: Mar 2008
Posts: 51

Rep: Reputation: 15
need to change variable in func, but var local...


so the following doesnt work as i understand it because functions have local variables s the nbr=0 is a different variable with the same name.

so how do i get around this?

Code:
from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()
mygui.title("Calculator")

w = Label(mygui, text="Calc this if you can!")
w.pack()

nbr=0

def Calc(nstr):
    nbr = nbr + 1
    print "You need to seek help!",nbr
    

b = Button(mygui, text="1",command=lambda n=1:Calc(n))
b.pack()

mygui.mainloop()
 
Old 04-08-2008, 04:23 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
You can 'solve' it this way:
Code:
nbr = 0

def Calc(nstr):
    global nbr
    nbr = nbr + 1
But I wouldn't recommend this. Use a class instead, and make Calc() a mehtod of that class.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
touch /var/lock/subsys/local <<-- Why? cl2imson Linux - Newbie 12 06-26-2011 02:35 PM
How can I initialize local/auto variable to zero rahul_kulkarni Linux - General 2 09-07-2007 02:14 PM
Memory for local variable in C on stack shivaligupta Programming 10 01-20-2007 01:22 AM
difference between function declaration: func() and func(void) koyi Programming 4 11-19-2005 10:19 AM
can thread return a local variable? iclinux Programming 1 01-13-2005 11:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration