Home > database >  Get a variable float value from GUI slider
Get a variable float value from GUI slider

Time:10-09

Hope you are all doing fine.

I am trying to make a code where I input variable values by using a slider in a GUI. I can get the slider to work and generate the desired values but I can not get to assign the generated value to a variable. I am sure its a simple mistake I am not understanding that probably has to do with the type of value the scale generates but I can't get to fix it.

Here is the code I attempted to do.

import math
import sympy
import scipy
from sympy import *
from scipy import optimize
from tkinter import *

def Compute():
    return scaleThetaB.get()

window = Tk()

window.title("Variable Selection")
window.geometry('400x300')
window['bg'] = '#D99D08'

scaleThetaB = Scale(window,from_=0, to=150,length=1000,orient=HORIZONTAL,font=('Arial,20'),tickinterval=10,showvalue=1,resolution=0.1,troughcolor='#990101')
scaleThetaB.pack()
button=Button(window,text='Compute',command=Compute)
button.pack()

window.mainloop()

ThetaB=Compute()
print(ThetaB)

Here is a picture of the GUI: GUI

In this specific case, I would like the assign the value of the slider (79.1) to my variable ThetaB.

And here is the error message: Error Message

Thank you for trying and helping!

CodePudding user response:

To assign the value to a variable you can simply use global variables and global:

from tkinter import Tk, Scale, Button

theta_b = 0


def compute():
    global theta_b
    theta_b = scale_theta_b.get()


window = Tk()

window.title("Variable Selection")
window.geometry('400x300')
window['bg'] = '#D99D08'

scale_theta_b = Scale(
    window, from_=0, to=150, length=1000, orient='horizontal',
    font=('Arial', 20), tickinterval=10, showvalue=1, resolution=0.1,
    troughcolor='#990101'
)
scale_theta_b.pack()
button = Button(window, text='Compute', command=compute)
button.pack()

window.mainloop()

You don't need to use the global constants such as HORIZONTAL, they most of the time represent the same name string just lowercase so I would suggest using those, because you don't need to import anything additionally.

Why not use *? Well you import everything, but you may not know or forget all the names that you import, it can cause an issue where from two modules you import the same thing but the last import overrides the first so the name you want to use may not reference the object you think it references. In other words it helps keep the namespace clean.
(additional info: you should either import what you need, e.g. from module import Class1, func_1, var_2 and so on or import the whole module: import module then You can also use an alias: import module as md or sth like that)

Also:
I strongly suggest following PEP 8 - Style Guide for Python Code. Function and variable names should be in snake_case, class names in CapitalCase. Don't have space around = if it is used as a part of keyword argument (func(arg='value')) but have space around = if it is used for assigning a value (variable = 'some value'). Have space around operators ( -/ etc.: value = x y(except here value = x y)). Have two blank lines around function and class declarations.

  • Related