【问题标题】:how can i make this variable t global , so that it can be used in all functions [duplicate]我怎样才能使这个变量成为全局变量,以便它可以在所有函数中使用[重复]
【发布时间】:2016-06-23 11:44:45
【问题描述】:

我使用t 作为全局并从s1,s2 函数中分配了t 值 但在person1 之后,它不会转到person2。错误如下

File "C:\Users\Teja kaipa\Desktop\estimation.py", line 42, in person1
if (t>1):
    NameError: name 't' is not defined
global t

代码:

def s1():
    t=1
    return t
def s2():
    t=2
    return t
def s3():
    t=3

def person1():
    output = 0
    val1 = int(r1e1.get())
    val2 = int(r1w1.get())
    if ((val1-val2)==0):
        output = 1+2*val1
    else:
        output = -2*abs(val1-val2)
    r1n1m.delete(0, END)
    r1n1m.insert(4,str(output))    
    if (t>1):
        person2()

def person2():
    val1= int(r1e2.get())
    val2= int(r1w2.get())
    if ((val1-val2)==0):
        output = 1+2*val1
    else:
        output = -2*abs(val1-val2)
    r1n2m.delete(0, END)
    r1n2m.insert(4,str(output))    
    if (t>2):
        person3()

【问题讨论】:

  • 是的,谢谢,它看起来确实有点相似。但我不能完全匹配我刚开始的时候。

标签: python function tkinter global-variables


【解决方案1】:

在要修改全局变量的每个模块中,明确指定t 为全局变量。

def func1():
    global t
    t = 3

def func2():
    global t
    t = [1,2,3]

def main():
    global t
    func1()
    print t
    func2()
    print t

输出应该是:

3
[1, 2, 3]

【讨论】:

  • 谢谢兄弟,我是初学者。我在网上学习东西,所以我错过了一些小鬼的东西。感谢您的支持:)
猜你喜欢
  • 2014-08-19
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
  • 2020-12-26
  • 1970-01-01
相关资源
最近更新 更多