Python Forum
[Tkinter] Take the variable of a cursor class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Take the variable of a cursor class
#1
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
Reply
#2
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()
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I pass a variable to another class, especially in this situation? Jionni 4 8,053 Feb-21-2021, 02:40 PM
Last Post: deanhystad
  Getting a variable from one class to another class menator01 6 3,153 Apr-26-2020, 04:36 PM
Last Post: Larz60+
  Updating a variable within a class MC2020 2 2,587 Apr-17-2020, 11:31 AM
Last Post: MC2020
  [Tkinter] Finding out what's under the cursor sabresong 3 2,714 Mar-28-2020, 02:52 PM
Last Post: sabresong
  [Tkinter] Update variable between class/frame edwin6938 6 4,546 Nov-22-2019, 08:13 AM
Last Post: edwin6938
  [Tkinter] Modifications on cursor Zyxcel314 3 2,999 Jun-04-2018, 04:36 PM
Last Post: Larz60+
  [Tkinter] Problem of cursor force_tranquille 0 2,131 May-07-2018, 09:20 AM
Last Post: force_tranquille
  [Tkinter] Scroll at cursor position lollo 4 6,254 Jan-31-2018, 11:33 PM
Last Post: lollo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020