更新全局/局部变量的问题

时间:2016-07-19 13:46:20

标签: python global-variables

看看下面的示例代码。有一个主窗口,它调用函数fun2来显示变量a的值(作为带文本的标签)。同时,主窗口上的菜单项启动带有两个按钮的辅助窗口(功能fun1)。我试图找到一种方法来通过这些按钮更改a的值。

如果我不包含全局变量a = 0(第6行),我会得到一个“全局名称'a'未定义”错误。如果我确实包含它(如本例所示),则窗口按钮对变量没有任何影响(它仍为a = 0)。我尝试在global a中使用命令fun1,但没有效果。我也尝试在fun2中使用相同的命令,但我仍然无法使其工作。

有什么想法吗?我做错了什么?

import Tkinter
import random
import time
from Tkinter import *

a = 0

def fun1():
    win2 = Toplevel()
    win2.geometry("300x300")
    def var_yes():
        a = 1
        win2.after(500, lambda: win2.destroy())
    def var_no():
        a = 0
        win2.after(500, lambda: win2.destroy())
    button1 = Tkinter.Button(win2, text ="1", command = var_yes).pack()
    button2 = Tkinter.Button(win2, text ="2", command = var_no).pack()

def fun2():
    value = 0
    if a == 1:
        value = a
    granresult = "{}".format(value)
    lbl.configure(text = str(granresult))


def gui_stuff(window):
    global lbl
    window.configure(background="#000000")
    window.geometry("700x500")
    lbl = Tkinter.Label(window, height=5, anchor = "c", text="Label", bg="#000000", fg="#ffffff", font=("Helvetica", 22))
    lbl.pack()
    fun2()


window = Tkinter.Tk()
menubar = Menu(window)
filemenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Program", menu=filemenu)
filemenu.add_command(label="test", command=fun1)
window.config(menu=menubar)
gui_stuff(window)
window.update()
winx1 = window.winfo_rootx()
winy1 = window.winfo_rooty()
winh1 = window.winfo_height()
window.mainloop()

2 个答案:

答案 0 :(得分:1)

global语句必须在分配给变量的函数中使用。它不会延续到子功能中,这就是它在fun1中没有效果的原因。您必须将其添加到var_yesvar_no

答案 1 :(得分:0)

你做错了是认为全局变量是一个好主意。然后不了解如何使用它们。在python中,您可以在模块级别范围内通过赋值创建全局。您已通过创建值并将其调用a

来完成此操作

在每个需要编写“a”的函数中,您需要声明为全局函数。见代码

import Tkinter
import random
import time
from Tkinter import *

a = 0

def fun1():
    win2 = Toplevel()
    win2.geometry("300x300")
    def var_yes():
        global a
        a = 1
        win2.after(500, lambda: win2.destroy())
    def var_no()
        global a
        a = 0
        win2.after(500, lambda: win2.destroy())
    button1 = Tkinter.Button(win2, text ="1", command = var_yes).pack()
    button2 = Tkinter.Button(win2, text ="2", command = var_no).pack()

def fun2():
    global a
    value = 0
    if a == 1:
        value = a
    granresult = "{}".format(value)
    lbl.configure(text = str(granresult))


def gui_stuff(window):
    # the next line refers to a global which doesn't exist
    global lbl
    window.configure(background="#000000")
    window.geometry("700x500")
    lbl = Tkinter.Label(window, height=5, anchor = "c", text="Label", bg="#000000", fg="#ffffff", font=("Helvetica", 22))
    lbl.pack()
    fun2()


window = Tkinter.Tk()
menubar = Menu(window)
filemenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Program", menu=filemenu)
filemenu.add_command(label="test", command=fun1)
window.config(menu=menubar)
gui_stuff(window)
window.update()
winx1 = window.winfo_rootx()
winy1 = window.winfo_rooty()
winh1 = window.winfo_height()
window.mainloop()