无法将 tkinter 滑块正确绑定到函数
Unable to bind tkinter slider properly to functions
我正在尝试制作一个简单的城市生成器。我已经成功地制作了指定高度的积木。
此外,我已经能够编写一个脚本来使用滑块更改块的高度。
问题
- 我不能同时做这两件事。 (我想我可以解决这个问题,因为错误只是关于赋值前的引用)
- 我想使用水平滑块来增加建筑物的数量(可视化为通过减小宽度来增加 x 轴上的条形数量)。但是这段代码根本不起作用,我的意思是,它甚至没有引发错误。
到目前为止的代码
from tkinter import *
import random
bars = 5
margin = 30
heights= [5, 2, 1, 8, 4]
blocks = []
length = 600
breadth = 360
def draw():
aligner = margin
width = (breadth - margin*2)/bars
for height in heights :
block = {"height" : height }
block['rect'] = canvas.create_rectangle(aligner,550-50*height,aligner+width,550, fill="green")
block['color'] = 'green'
blocks.append(block)
aligner += width
root.update()
def wid(e):
bars = int(e)
blocks = []
heights = []
for i in range(bars):
h = random.random()*10
heights.append(h)
draw()
def h_rand(e):
factor = e
heights = [i * factor for i in heights]
draw()
root = Tk()
width = 60
aligner = 30
slider_y = Scale(root, from_=1 , to=8, bg="blue",command = h_rand)
slider_y.pack(side=LEFT)
canvas = Canvas(root,height=length,width=breadth)
canvas.pack()
for height in heights :
block = {"height" : height }
block['rect'] = canvas.create_rectangle(aligner,550-50*height,aligner+width,550, fill="green")
block['color'] = 'green'
blocks.append(block)
aligner += width
slider_x = Scale(root, from_=50 , to=200, orient = HORIZONTAL, bg="blue",command = wid)
slider_x.pack(side=TOP)
root.mainloop()
对于 "not so descriptive formatting" 的问题,我深表歉意(很高兴在评论中提供更多详细信息)。它困扰了我一段时间,我很沮丧。
问题是wid
中的heights
是局部变量。您正在更改函数内部的值,但全局变量保持不变。
您要么需要在函数内部将 heights
声明为全局变量,要么使用全局变量而不用新列表替换它。
要将其声明为全局,请在要修改的函数顶部添加 global heights
:
def wid(e):
global heights, blocks
...
您可以使用列表的 clear
方法来删除内容,而无需重置变量本身。为此,请将 heights = []
替换为 heights.clear()
并将 blocks
替换为相同的 blocks
:
def wid(e):
bars = int(e)
print("bars:", bars)
blocks.clear()
heights.clear()
...
您还遇到了在创建新项目之前没有删除旧 canvas 项目的问题。您的 draw
函数需要销毁旧元素。您可以使用 canvas.delete("all")
来做到这一点。
我正在尝试制作一个简单的城市生成器。我已经成功地制作了指定高度的积木。
此外,我已经能够编写一个脚本来使用滑块更改块的高度。
问题
- 我不能同时做这两件事。 (我想我可以解决这个问题,因为错误只是关于赋值前的引用)
- 我想使用水平滑块来增加建筑物的数量(可视化为通过减小宽度来增加 x 轴上的条形数量)。但是这段代码根本不起作用,我的意思是,它甚至没有引发错误。
到目前为止的代码
from tkinter import *
import random
bars = 5
margin = 30
heights= [5, 2, 1, 8, 4]
blocks = []
length = 600
breadth = 360
def draw():
aligner = margin
width = (breadth - margin*2)/bars
for height in heights :
block = {"height" : height }
block['rect'] = canvas.create_rectangle(aligner,550-50*height,aligner+width,550, fill="green")
block['color'] = 'green'
blocks.append(block)
aligner += width
root.update()
def wid(e):
bars = int(e)
blocks = []
heights = []
for i in range(bars):
h = random.random()*10
heights.append(h)
draw()
def h_rand(e):
factor = e
heights = [i * factor for i in heights]
draw()
root = Tk()
width = 60
aligner = 30
slider_y = Scale(root, from_=1 , to=8, bg="blue",command = h_rand)
slider_y.pack(side=LEFT)
canvas = Canvas(root,height=length,width=breadth)
canvas.pack()
for height in heights :
block = {"height" : height }
block['rect'] = canvas.create_rectangle(aligner,550-50*height,aligner+width,550, fill="green")
block['color'] = 'green'
blocks.append(block)
aligner += width
slider_x = Scale(root, from_=50 , to=200, orient = HORIZONTAL, bg="blue",command = wid)
slider_x.pack(side=TOP)
root.mainloop()
对于 "not so descriptive formatting" 的问题,我深表歉意(很高兴在评论中提供更多详细信息)。它困扰了我一段时间,我很沮丧。
问题是wid
中的heights
是局部变量。您正在更改函数内部的值,但全局变量保持不变。
您要么需要在函数内部将 heights
声明为全局变量,要么使用全局变量而不用新列表替换它。
要将其声明为全局,请在要修改的函数顶部添加 global heights
:
def wid(e):
global heights, blocks
...
您可以使用列表的 clear
方法来删除内容,而无需重置变量本身。为此,请将 heights = []
替换为 heights.clear()
并将 blocks
替换为相同的 blocks
:
def wid(e):
bars = int(e)
print("bars:", bars)
blocks.clear()
heights.clear()
...
您还遇到了在创建新项目之前没有删除旧 canvas 项目的问题。您的 draw
函数需要销毁旧元素。您可以使用 canvas.delete("all")
来做到这一点。