所以我对编码还不熟悉,但我正在尝试制作我的第一个完整应用程序。我的目标是让三个Tkinter scale小部件以随机生成的节奏显示每个对应音符(拨动音符、明确沉默或持续音符)的概率/权重。这就是说,NumPy的随机模块要求所有三个概率都必须等于一——然而,我仍然希望保留默认的起始值
我尝试过广泛使用.get()
和.set()
方法以及Scale变量,但都没有效果。
几天过去了,我的大脑很痛。我无法让他们中的任何人动态链接。也许我需要一个更面向对象的方法吗
我剥离了所有功能和不必要的GUI元素,以帮助隔离问题:
from tkinter import *
# def notes_comp():
# summ = (float(silence_weight.get()))+(float(sustain_weight.get()))
# diff = 1 - (float(note_weight.get())+summ)
# if float(note1_weight.get())+summ != 1:
# silence_weight.set(silence_weight.get()+diff/2)
# sustain_weight.set(sustain_weight.get()+diff/2)
def notes_comp(value):
notes_weight = var_1.set("value")
silence_set = var_2.get()
sustain_set = var_3.get()
for i in range(1 - notes_weight):
silence_set += .5*(1 - var_1.get())
sustain_set += .5*(1 - var_1.get())
# tkinter GUI
root = Tk(className=" Rhythm Generator")
# setting color variables
bg_color = "#1c1c1c"
rollover_color = "#d9d9d9"
var_1 = DoubleVar()
var_2 = DoubleVar()
var_3 = DoubleVar()
root.configure(bg=bg_color)
# creating master frames
top_frame = Frame(root, bg=bg_color)
top_frame.grid(row="0")
# creating sliders
note1_weight = Scale(top_frame, from_="1", to="0", resolution=".1", variable="var_1", command="notes_comp")
note1_weight.grid(column="2", row="0")
silence_weight = Scale(top_frame, from_="1", to="0", resolution=".1", variable="var_2")
silence_weight.grid(column="3", row="0")
sustain_weight = Scale(top_frame, from_="1", to="0", resolution=".1", variable="var_3")
sustain_weight.grid(column="4", row="0")
note1_weight.set(.1)
silence_weight.set(.1)
sustain_weight.set(.8)
root.mainloop()
这段代码解决了在移动
Scale
时运行notes_comp
的一些问题,但它并不能解决所有值之和等于1的问题在所有情况下都必须使用变量,而不是字符串
"var_1"
和"notes_comp"
对于每个
Scale
,我使用两个DoubleVar
-current_var
和previous_var
,这样我可以比较它是如何变化的,并尝试使用差异current_var - previous_var
来改变其他值。但这是一个问题,因为我不能将(current_var - previous_var)/2
添加到两者中,因为其中一个可能已经具有最大值。另一个问题是(current_var - previous_var)/2
可能给出值0.05
,但是Scale
有resolution=".1"
,所以它不能显示0.05
、0.15
、0.25
等等。如果我改变resolution=".05"
,那么(current_var - previous_var)/2
将给出值0.025
,Scale
将不会播放0.025
、0.075
、0.125
,等等另一个问题是(通常情况下)
float
值,这些值有时给出不同的结果,然后我们可以期望-即0.1 + 0.2 == 0.3
给出False
,但我们可以期望True
-因为计算机只保留一些近似值由于缩放小部件命令回调函数在缩放小部件发生更改时被调用,因此回调必须“关闭”,因为小部件被设置为避免调用回调函数的无限序列,因为小部件1的更改会影响小部件2&;3.
相关问题 更多 >
编程相关推荐