Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的按钮不会更改变量';s值。为什么?_Python_Tkinter - Fatal编程技术网

Python 我的按钮不会更改变量';s值。为什么?

Python 我的按钮不会更改变量';s值。为什么?,python,tkinter,Python,Tkinter,颜色是变量,当启动程序并按下按钮时,气泡仍保持蓝色。我该怎么办? 按钮不起作用或者为什么会发生这种情况?color='green'只设置局部变量color的值。函数结束时,变量消失。这并没有完成任何事情,因为你们从来并没有做过有价值的事情。@kindall击败了我。了解变量作用域:这是一个非常常见的问题,它总是欺骗新的程序员。关于这方面有很多教程,如果您想了解更多详细信息,下面的问题可能会有所帮助:id1变量使用了可能重复的变量颜色 from tkinter import * from rand

颜色是变量,当启动程序并按下按钮时,气泡仍保持蓝色。我该怎么办?
按钮不起作用或者为什么会发生这种情况?

color='green'
只设置局部变量
color
的值。函数结束时,变量消失。这并没有完成任何事情,因为你们从来并没有做过有价值的事情。@kindall击败了我。了解变量作用域:这是一个非常常见的问题,它总是欺骗新的程序员。关于这方面有很多教程,如果您想了解更多详细信息,下面的问题可能会有所帮助:id1变量使用了可能重复的变量颜色
from tkinter import *
from random import randint
from math import sqrt
def create_bubble():
    x = WIDTH + GAP
    y = randint(0, HEIGHT)
    r = randint(MIN_BUB_R, MAX_BUB_R)
    id1 = c.create_rectangle(x - r, y - r, x + r, y + r, outline="light blue", fill=color)
    bub_id.append(id1)
    bub_r.append(r)
    bub_speed.append(randint(1, MAX_BUB_SPD))
def buton():
    color = 'green'
HEIGHT = 500
WIDTH = 800
window = Tk()
window.title("Distrugatorul de bule")
c = Canvas(window, width=WIDTH, height=HEIGHT, bg="darkblue")
c.pack()
color = 'blue'
bub_id = list()
bub_r = list()
bub_speed = list()
MIN_BUB_R = 10
MAX_BUB_R = 30
MAX_BUB_SPD = 10
GAP = 100
b = Button(window, text="change", command = buton)
b.pack()
mainloop()
BUB_CHANCE = 10
if randint(1, BUB_CHANCE) == 1:
        create_bubble()