Python Forum
[Tkinter] Take the variable of a cursor class - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Take the variable of a cursor class (/thread-39403.html)



Take the variable of a cursor class - delcencen - Feb-12-2023

Hello, i try diffrent things beffor call your help but that doesn't work.
I would like to get the x value in my main code.
from tkinter import*
class CursseurBrightness(Frame):
    def __init__(self):
        Frame.__init__(self)
        def updateLabel(x):
            lab.configure(text='Curent value = ' + str(x))
        winCurs = Tk()
        Scale(winCurs,length=250,orient=HORIZONTAL,label ='Exposition :',troughcolor='dark grey',sliderlength=20,showvalue=10,from_=0,to=20,tickinterval=1,command=updateLabel).pack()
        lab=Label(winCurs)
        lab.pack()
        

cursBright=CursseurBrightness()
cursBright.mainloop()
brightness_factor0to20=cursBright

while True:


    print(brightness_factor0to20)
thanks for your help Wall


RE: Take the variable of a cursor class - Yoriz - Feb-12-2023

import tkinter as tk


class CursseurBrightness(tk.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.pack()
        tk.Scale(
            self,
            length=250,
            orient=tk.HORIZONTAL,
            label="Exposition :",
            troughcolor="dark grey",
            sliderlength=20,
            showvalue=True,
            from_=0,
            to=20,
            tickinterval=1,
            command=self.update_label,
        ).pack()
        self.lab = tk.Label(self)
        self.lab.pack()

    def update_label(self, current_value):
        self.lab.configure(text=f"Curent value = {current_value}")


app = tk.Tk()
curs_bright = CursseurBrightness(app)
curs_bright.mainloop()



RE: Take the variable of a cursor class - deanhystad - Feb-13-2023

A tkinter program needs a loop that calls update() for the window. You could do this:
import tkinter as tk

class Window(tk.Tk):
    def __init__(self):
        super().__init__()
        self.slider = tk.Scale(self, sliderlength=20)
        self.slider.pack(padx=30, pady=30)
        self.slider.pack()

x = Window()
while True:
    x.update()
    print(x.slider.get())
This will work. It prints the value of the slider, but it would be difficult to extend the application.

Instead of writing your own update loop, tkinter provides one for you; mainloop()
class Window(tk.Tk):
    def __init__(self):
        super().__init__()
        self.sliders = (
            tk.Scale(self, to=255, command=self.slider_changed),
            tk.Scale(self, to=255, command=self.slider_changed),
            tk.Scale(self, to=255, command=self.slider_changed),
        )
        padx = 30
        for slider in self.sliders:
            slider.pack(side=tk.LEFT, padx=padx, pady=30)
            padx = (0, 30)

    def slider_changed(self, _):
        color = 0
        for slider in self.sliders:
            color = color * 256 + slider.get()
        print(f'{color:X}')

Window().mainloop()
The problem with mainloop() is it has to be the only forever loop in your application. If you need another loop that runs for a long time, you need to run it in a separate thread or process, or you need to change the loop to use event logic.
import tkinter as tk


class Window(tk.Tk):
    def __init__(self):
        super().__init__()
        self.sliders = (
            tk.Scale(self, to=255),
            tk.Scale(self, to=255),
            tk.Scale(self, to=255),
        )
        padx = 30
        for slider in self.sliders:
            slider.pack(side=tk.LEFT, padx=padx, pady=30)
            padx = (0, 30)

    def print_color(self, _=None):
        color = 0
        for slider in self.sliders:
            color = color * 256 + slider.get()
        print(f'{color:X}')
        self.after(1000, self.print_color)

w = Window()
w.print_color()
w.mainloop()
The above example changes a loop that prints the slider values to a periodic event that is run using a timer.