如何将带有刻度的新值传输到另一个函数?

时间:2019-01-06 13:15:44

标签: python python-2.7 tkinter tkinter-scale

我使用的是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()在参数中包含了scale所获得的新值。

1 个答案:

答案 0 :(得分:1)

您可以将vol_piano设置为全局变量。每当在Scale函数中移动maj()时,都要更新其值。每当单击画布时,只需打印出vol_piano的值即可。

import tkinter as tk

fenetre = tk.Tk()

vol_piano = None

def other(ev):
    global vol_piano
    print(vol_piano)

def maj(newvalue):
    global vol_piano
    vol_piano = newvalue

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()