我如何解决 python 的全局变量问题
How do I work around python's global variable problem
我目前正在制作一个刽子手游戏,我有一个全局变量来指示绘制刽子手的哪一部分。问题是我需要在函数中更改此变量 (drawRate
) 的值,因为稍后我将在不同的函数中需要它,但 python 不允许我这样做。有什么办法可以解决这个问题吗?
import tkinter as tk
import turtle
import string
from functools import partial
from draw_hangman import drawHangman
word = 'hello'
shown_text = list('-'*len(word))
draw = drawHangman()
drawRate = 0
def whenPressed(button, text):
button.config(state = 'disabled')
ind = []
local_word = list(word)
for i in local_word :
if i == text:
trash = local_word.index(i)
ind.append(trash)
local_word.pop(trash)
local_word.insert(trash, '-')
if len(ind) != 0:
for i in ind:
shown_text.pop(i)
shown_text.insert(i, text)
lab.config(text = ''.join(shown_text))
for i in shown_text:
if i == '-':
trash = True
if trash != True:
print('You Won!')
else:
trash = draw[drawRate]
exec(trash)
drawRate+=1
root = tk.Tk()
t = turtle.Turtle()
alphabet = list(string.ascii_lowercase)
lab = tk.Label(root, text = '-'*len(word), font = (None, 15), width = 30)
lab.grid(row = 0, columnspan = 13)
for i in alphabet:
btn = tk.Button(root, text=i)
command = partial(whenPressed, btn, i)
btn.config(command=command)
row = (alphabet.index(i) // 13)+1
column = alphabet.index(i) % 13
btn.grid(row=row, column=column, sticky="news")
变量draw
是一个列表,其中包含绘制刽子手图形的命令:
draw = [
'''t.penup()
t.fd(200)
t.rt(90)
t.fd(200)''',
'''t.down()
t.lt(270)
t.fd(400)''',
'''t.rt(90)
t.fd(400)''',
'''t.rt(90)
t.fd(300)''',
'''t.rt(90)
t.fd(75)
t.dot(75)''',
't.fd(100)',
'''t.lt(90)
t.fd(60)''',
'''t.back(120)
t.fd(60)
t.rt(90)''',
'''t.fd(75)
t.lt(30)
t.fd(100)''',
'''t.back(100)
t.rt(60)
t.fd(100)''']
尝试在 whenPressed 函数的顶部将 drawRate 定义为 global
喜欢
import turtle
import string
from functools import partial
from draw_hangman import drawHangman
word = 'hello'
shown_text = list('-'*len(word))
draw = drawHangman()
drawRate = 0
def whenPressed(button, text):
global drawRate
button.config(state = 'disabled')
ind = []
local_word = list(word)
for i in local_word :
if i == text:
trash = local_word.index(i)
ind.append(trash)
local_word.pop(trash)
local_word.insert(trash, '-')
if len(ind) != 0:
for i in ind:
shown_text.pop(i)
shown_text.insert(i, text)
lab.config(text = ''.join(shown_text))
for i in shown_text:
if i == '-':
trash = True
if trash != True:
print('You Won!')
else:
trash = draw[drawRate]
exec(trash)
drawRate+=1
root = tk.Tk()
t = turtle.Turtle()
alphabet = list(string.ascii_lowercase)
lab = tk.Label(root, text = '-'*len(word), font = (None, 15), width = 30)
lab.grid(row = 0, columnspan = 13)
for i in alphabet:
btn = tk.Button(root, text=i)
command = partial(whenPressed, btn, i)
btn.config(command=command)
row = (alphabet.index(i) // 13)+1
column = alphabet.index(i) % 13
btn.grid(row=row, column=column, sticky="news"```
您必须在 whenPressed()
函数中将此变量声明为 global,如下所示:
def whenPressed(button, text):
global drawRate
...
我是新手,如果这不正确请提前原谅我但是你能不能在函数开始时在你的全局变量中declare/bring:global drawRate
def whenPressed(button, text):
global drawRate
button.config(state = 'disabled')
ind = []
local_word = list(word)
for i in local_word :
if i == text:
trash = local_word.index(i)
ind.append(trash)
local_word.pop(trash)
local_word.insert(trash, '-')
if len(ind) != 0:
for i in ind:
shown_text.pop(i)
shown_text.insert(i, text)
lab.config(text = ''.join(shown_text))
for i in shown_text:
if i == '-':
trash = True
if trash != True:
print('You Won!')
else:
trash = draw[drawRate]
exec(trash)
drawRate+=1
我目前正在制作一个刽子手游戏,我有一个全局变量来指示绘制刽子手的哪一部分。问题是我需要在函数中更改此变量 (drawRate
) 的值,因为稍后我将在不同的函数中需要它,但 python 不允许我这样做。有什么办法可以解决这个问题吗?
import tkinter as tk
import turtle
import string
from functools import partial
from draw_hangman import drawHangman
word = 'hello'
shown_text = list('-'*len(word))
draw = drawHangman()
drawRate = 0
def whenPressed(button, text):
button.config(state = 'disabled')
ind = []
local_word = list(word)
for i in local_word :
if i == text:
trash = local_word.index(i)
ind.append(trash)
local_word.pop(trash)
local_word.insert(trash, '-')
if len(ind) != 0:
for i in ind:
shown_text.pop(i)
shown_text.insert(i, text)
lab.config(text = ''.join(shown_text))
for i in shown_text:
if i == '-':
trash = True
if trash != True:
print('You Won!')
else:
trash = draw[drawRate]
exec(trash)
drawRate+=1
root = tk.Tk()
t = turtle.Turtle()
alphabet = list(string.ascii_lowercase)
lab = tk.Label(root, text = '-'*len(word), font = (None, 15), width = 30)
lab.grid(row = 0, columnspan = 13)
for i in alphabet:
btn = tk.Button(root, text=i)
command = partial(whenPressed, btn, i)
btn.config(command=command)
row = (alphabet.index(i) // 13)+1
column = alphabet.index(i) % 13
btn.grid(row=row, column=column, sticky="news")
变量draw
是一个列表,其中包含绘制刽子手图形的命令:
draw = [
'''t.penup()
t.fd(200)
t.rt(90)
t.fd(200)''',
'''t.down()
t.lt(270)
t.fd(400)''',
'''t.rt(90)
t.fd(400)''',
'''t.rt(90)
t.fd(300)''',
'''t.rt(90)
t.fd(75)
t.dot(75)''',
't.fd(100)',
'''t.lt(90)
t.fd(60)''',
'''t.back(120)
t.fd(60)
t.rt(90)''',
'''t.fd(75)
t.lt(30)
t.fd(100)''',
'''t.back(100)
t.rt(60)
t.fd(100)''']
尝试在 whenPressed 函数的顶部将 drawRate 定义为 global
喜欢
import turtle
import string
from functools import partial
from draw_hangman import drawHangman
word = 'hello'
shown_text = list('-'*len(word))
draw = drawHangman()
drawRate = 0
def whenPressed(button, text):
global drawRate
button.config(state = 'disabled')
ind = []
local_word = list(word)
for i in local_word :
if i == text:
trash = local_word.index(i)
ind.append(trash)
local_word.pop(trash)
local_word.insert(trash, '-')
if len(ind) != 0:
for i in ind:
shown_text.pop(i)
shown_text.insert(i, text)
lab.config(text = ''.join(shown_text))
for i in shown_text:
if i == '-':
trash = True
if trash != True:
print('You Won!')
else:
trash = draw[drawRate]
exec(trash)
drawRate+=1
root = tk.Tk()
t = turtle.Turtle()
alphabet = list(string.ascii_lowercase)
lab = tk.Label(root, text = '-'*len(word), font = (None, 15), width = 30)
lab.grid(row = 0, columnspan = 13)
for i in alphabet:
btn = tk.Button(root, text=i)
command = partial(whenPressed, btn, i)
btn.config(command=command)
row = (alphabet.index(i) // 13)+1
column = alphabet.index(i) % 13
btn.grid(row=row, column=column, sticky="news"```
您必须在 whenPressed()
函数中将此变量声明为 global,如下所示:
def whenPressed(button, text):
global drawRate
...
我是新手,如果这不正确请提前原谅我但是你能不能在函数开始时在你的全局变量中declare/bring:global drawRate
def whenPressed(button, text):
global drawRate
button.config(state = 'disabled')
ind = []
local_word = list(word)
for i in local_word :
if i == text:
trash = local_word.index(i)
ind.append(trash)
local_word.pop(trash)
local_word.insert(trash, '-')
if len(ind) != 0:
for i in ind:
shown_text.pop(i)
shown_text.insert(i, text)
lab.config(text = ''.join(shown_text))
for i in shown_text:
if i == '-':
trash = True
if trash != True:
print('You Won!')
else:
trash = draw[drawRate]
exec(trash)
drawRate+=1