【发布时间】:2021-05-26 11:55:04
【问题描述】:
我有一个代码,它可以从列表中生成 12 个带有名称的按钮。我想将这些名称传递给一个函数,但目前只记住按钮的最后一个文本,并且它被用于所有按钮。 有没有办法(除了手动创建所有按钮)让它工作,这意味着正确的按钮名称将被进一步传递?
import tkinter as tk
from tkinter import *
notes_list = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#']
c = 0
r = 0
def notes(note):
major_scale_steps = [2,2,1,2,2,2,1]
minor_scale_notes = [2,1,2,2,1,2,2]
#find where provided note is located and rearrange notes_list accordingly
ind = notes_list.index(note)
n_tran = notes_list[ind:]+notes_list[:ind]
#gather notes using major_scale_steps
major_scale = [n_tran[0],n_tran[2],n_tran[4],n_tran[5],n_tran[7],n_tran[9], n_tran[11], n_tran[0]]
minor_scale = [n_tran[0],n_tran[2],n_tran[3],n_tran[5],n_tran[7],n_tran[8], n_tran[10], n_tran[0]]
return major_scale
def clicked():
te = notes(i.config('text')[-1])
#place label with results
test = tk.Label(window, text=te)
test.place(x=100, y=150)
window = tk.Tk()
window.title('Guitar notes')
window.geometry('320x250')
#create buttons
for i in notes_list:
i = Button(window, text=i, command=clicked)
i.config(height=2, width=10)
i.grid(column=c, row=r)
c+=1
if c == 4:
c=0
r+=1
window.mainloop()
【问题讨论】:
-
把你的
test标签放在func()之外。config在函数中。在您的情况下,我认为您需要删除/销毁place_slaves。你已经制作了所有这些索引。这将始终是相同的结果。你需要改变它。有不同的方式,有shuffle,random等。还有list方法,反向可以玩