【问题标题】:How do I work around python's global variable problem如何解决python的全局变量问题
【发布时间】:2021-04-18 12:00:21
【问题描述】:

我目前正在制作一个刽子手游戏,我有一个全局变量来指示要绘制刽子手的哪一部分。问题是我需要在函数中更改此变量 (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)''']

【问题讨论】:

    标签: python python-3.x global-variables


    【解决方案1】:

    您必须在 whenPressed() 函数中将此变量声明为 global,如下所示:

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

    【讨论】:

      【解决方案2】:

      我是新手,如果这不正确,请提前原谅我,但你能不能在函数开始时声明/引入你的全局变量: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
      

      【讨论】:

        【解决方案3】:

        尝试在 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"```
        

        【讨论】:

        • 我之前尝试过这个,但它引发了一个错误,指出在分配之前已经引用了局部变量“drawRate”。现在它正在工作。
        猜你喜欢
        • 1970-01-01
        • 2014-05-15
        • 1970-01-01
        • 2022-10-24
        • 2022-11-16
        • 2014-11-11
        • 1970-01-01
        • 2020-04-21
        • 1970-01-01
        相关资源
        最近更新 更多