簡體   English   中英

如何解決python的全局變量問題

[英]How do I work around python's global variable problem

我目前正在制作一個劊子手游戲,我有一個全局變量來指示要繪制劊子手的哪一部分。 問題是我需要在 function 中更改此變量( drawRate )的值,因為稍后我將在不同的 function 中需要它,但 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() function 中將此變量聲明為全局變量,如下所示:

def whenPressed(button, text):
    global drawRate
    ...

我是新手,如果這是不正確的,請提前原諒我,但你能不能在 function 開頭聲明/引入你的全局變量: 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

嘗試在 whenPressed function 的頂部將 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"```

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM