CaptainCoding7
CaptainCoding7

Reputation: 29

How to transmit a new value got with a scale to another function?

I'm on Python2.7 and I try to transmit a value got from a scale, to another function which has to respond to a click.

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

The problem is that I can't use return because my function maj() has in argument the new value got with scale.

Upvotes: 0

Views: 59

Answers (1)

Miraj50
Miraj50

Reputation: 4407

You can make vol_piano a global variable. Update its value whenever Scale is moved inside the maj() function. Whenever the canvas is clicked, just print out the value of 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()

Upvotes: 1

Related Questions