如何将通过比例获得的新值传递给另一个函数?
How to transmit a new value got with a scale to another function?
我在 Python2.7,我尝试将从 scale
获得的值传输到另一个必须响应点击的函数。
from tkinter import *
fenetre = Tk()
def other(ev):
m=2
l=3
vol_piano=maj()
print(vol_piano)
def maj(newvalue):
vol_piano = newvalue
print(vol_piano)
return vol_piano
value = DoubleVar()
scale = Scale(fenetre, variable=value, orient ='vertical', from_ = 0, to= 100,
resolution = 1, tickinterval= 5, length=400, label='Volume Piano',command=maj)
scale.pack(side=RIGHT)
canvas = Canvas(fenetre, width=100, height=400, bg="white")
curseur1 = canvas.create_line(0, 0, 0, 0)
canvas.pack(side=LEFT)
canvas.bind("<Button-1>", other)
fenetre.mainloop()
问题是我无法使用 return,因为我的函数 maj()
在参数中有新值随比例获得。
您可以将 vol_piano
设为全局变量。每当 Scale
移入 maj()
函数时更新它的值。每当单击 canvas 时,只需打印出 vol_piano
.
的值
import tkinter as tk
fenetre = tk.Tk()
<b>vol_piano = None</b>
def other(ev):
<b>global vol_piano</b>
print(vol_piano)
def maj(newvalue):
<b>global vol_piano
vol_piano = newvalue</b>
value = tk.DoubleVar()
scale = tk.Scale(fenetre, variable=value, orient ='vertical', from_ = 0, to= 100,
resolution = 1, tickinterval= 5, length=400, label='Volume Piano',command=maj)
scale.pack(side="right")
canvas = tk.Canvas(fenetre, width=100, height=400, bg="white")
canvas.pack(side="left")
canvas.bind("<Button-1>", other)
fenetre.mainloop()
我在 Python2.7,我尝试将从 scale
获得的值传输到另一个必须响应点击的函数。
from tkinter import *
fenetre = Tk()
def other(ev):
m=2
l=3
vol_piano=maj()
print(vol_piano)
def maj(newvalue):
vol_piano = newvalue
print(vol_piano)
return vol_piano
value = DoubleVar()
scale = Scale(fenetre, variable=value, orient ='vertical', from_ = 0, to= 100,
resolution = 1, tickinterval= 5, length=400, label='Volume Piano',command=maj)
scale.pack(side=RIGHT)
canvas = Canvas(fenetre, width=100, height=400, bg="white")
curseur1 = canvas.create_line(0, 0, 0, 0)
canvas.pack(side=LEFT)
canvas.bind("<Button-1>", other)
fenetre.mainloop()
问题是我无法使用 return,因为我的函数 maj()
在参数中有新值随比例获得。
您可以将 vol_piano
设为全局变量。每当 Scale
移入 maj()
函数时更新它的值。每当单击 canvas 时,只需打印出 vol_piano
.
import tkinter as tk
fenetre = tk.Tk()
<b>vol_piano = None</b>
def other(ev):
<b>global vol_piano</b>
print(vol_piano)
def maj(newvalue):
<b>global vol_piano
vol_piano = newvalue</b>
value = tk.DoubleVar()
scale = tk.Scale(fenetre, variable=value, orient ='vertical', from_ = 0, to= 100,
resolution = 1, tickinterval= 5, length=400, label='Volume Piano',command=maj)
scale.pack(side="right")
canvas = tk.Canvas(fenetre, width=100, height=400, bg="white")
canvas.pack(side="left")
canvas.bind("<Button-1>", other)
fenetre.mainloop()