chun9321md
Posts: 3
Joined: Tue Aug 06, 2019 10:57 am

How to using a value from spinbox

Wed Aug 07, 2019 11:53 am

How to use a value from spinbox? Even though i set the spinbox value (>1), the "print('turning on'+spinsee1)" command is printing "turning on0" (the default value spinbox value=0). So, how to use the exact value, for example: when i set the value as 30, the print is showing turning on30?

Code: Select all

from tkinter import *
import tkinter as tk 
import time
root = Tk()

class App(Frame):
    def __init__(self,master=None):
        Frame.__init__(self, master)
        self.master = master
        self.label = Label(text="", fg="Red", font=("Helvetica", 10))
        self.label.grid(row=6, column=1)
        self.update_clock()

    def update_clock(self):
        now = time.strftime("%H:%M:%S")
        self.label.configure(text=now)
        self.after(1000, self.update_clock)

app=App(root)
root.wm_title("Jotnar controller_version 1.0 beta")
Label(root, text='Current time:').grid(row=6, column=0) 
root.geometry("300x250")
root.after(1000, app.update_clock)

Label(root, text='Percentage(%)').place(x=140, y=0)
Label(root, text='1').place(x=10, y=30)
Label(root, text='2').place(x=10, y=60)
Label(root, text='3').place(x=10, y=90)
Label(root, text='4').place(x=10, y=120)
Label(root, text='5').place(x=10, y=150)
Label(root, text='6').place(x=10, y=180) 

spin1 = Spinbox(root, from_=0, to=100, width=5)
global spinsee1
spin1.place(x=150, y=30)
spinsee1=spin1.get()

spin2 = Spinbox(root, from_=0, to=100, width=5)
global spinsee2
spin2.place(x=150, y=60)
spinsee2=spin2.get()

spin3 = Spinbox(root, from_=0, to=100, width=5)
spin3.place(x=150, y=90)
global spinsee3
spinsee3=spin3.get()

spin4 = Spinbox(root, from_=0, to=100, width=5)
spin4.place(x=150, y=120)
global spinsee4
spinsee4=spin4.get()

spin5 = Spinbox(root, from_=0, to=100, width=5)
spin5.place(x=150, y=150)
global spinsee5
spinsee5=spin5.get()

spin6 = Spinbox(root, from_=0, to=100, width=5)
spin6.place(x=150, y=180)
global spinsee6
spinsee6=spin6.get()
 
def toggle1():
    if var1.get() == "ON":
        print('turning on'+spinsee1)
    else:
        print("turning off...") 

var1 = tk.StringVar()
toggle = tk.Checkbutton(root, onvalue="ON", offvalue="OFF", width=4,
                        indicatoron=False, 
                        variable=var1, textvariable=var1,
                        selectcolor="green", background="red",
                        command=toggle1)

var1.set("OFF")
toggle.place(x=220, y=30)

def toggle2():
    if var2.get() == "ON":
        print('turning on'+spinsee2)
    else:
        print("turning off...")

var2 = tk.StringVar()
toggle = tk.Checkbutton(root, onvalue="ON", offvalue="OFF", width=4,
                        indicatoron=False, 
                        variable=var2, textvariable=var2,
                        selectcolor="green", background="red",
                        command=toggle2)

var2.set("OFF")
toggle.place(x=220, y=60)

def toggle3():
    if var3.get() == "ON":
        print('turning on'+spinsee3)
    else:
        print("turning off...")

var3 = tk.StringVar()
toggle = tk.Checkbutton(root, onvalue="ON", offvalue="OFF", width=4,
                        indicatoron=False, 
                        variable=var3, textvariable=var3,
                        selectcolor="green", background="red",
                        command=toggle3)

var3.set("OFF")
toggle.place(x=220, y=90)

def toggle4():
    if var4.get() == "ON":
        print('turning on'+spinsee4)
    else:
        print("turning off...")

var4 = tk.StringVar()
toggle = tk.Checkbutton(root, onvalue="ON", offvalue="OFF", width=4,
                        indicatoron=False, 
                        variable=var4, textvariable=var4,
                        selectcolor="green", background="red",
                        command=toggle4)

var4.set("OFF")
toggle.place(x=220, y=120)

def toggle5():
    if var5.get() == "ON":
        print('turning on'+spinsee5)
    else:
        print("turning off...")

var5 = tk.StringVar()
toggle = tk.Checkbutton(root, onvalue="ON", offvalue="OFF", width=4,
                        indicatoron=False, 
                        variable=var5, textvariable=var5,
                        selectcolor="green", background="red",
                        command=toggle5)

var5.set("OFF")
toggle.place(x=220, y=150)

def toggle6():
    if var6.get() == "ON":
        print('turning on'+spinsee6)
    else:
        print("turning off...")

var6 = tk.StringVar()
toggle = tk.Checkbutton(root, onvalue="ON", offvalue="OFF", width=4,
                        indicatoron=False, 
                        variable=var6, textvariable=var6,
                        selectcolor="green", background="red",
                        command=toggle6)

var6.set("OFF")
toggle.place(x=220, y=180)

mainloop()

User avatar
topguy
Posts: 5759
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: How to using a value from spinbox

Wed Aug 07, 2019 12:34 pm

Please put Python questions to the python sub-forum.. the General Discussion forum is overcluttered as it is.

Also google..
https://stackoverflow.com/questions/438 ... om-spinbox

scotty101
Posts: 3711
Joined: Fri Jun 08, 2012 6:03 pm

Re: How to using a value from spinbox

Wed Aug 07, 2019 1:06 pm

You need to "get" the value directly before you use it. At the moment you only get the value once when the program starts

See below example

Code: Select all

def toggle1():
    if var1.get() == "ON":
        spinsee = spin1.get()
        print('turning on'+spinsee1)
    else:
        print("turning off...") 
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12212
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: How to using a value from spinbox

Wed Aug 07, 2019 2:55 pm

moved to python

User avatar
MrYsLab
Posts: 372
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: How to using a value from spinbox

Wed Aug 07, 2019 4:20 pm

In general, one of the main reasons to use a class is that allows one to avoid using globals and organizes the code so that it is easier to read and debug.
Therefore, I have reformatted your script as follows. It should now function properly.

Code: Select all

from tkinter import *
import tkinter as tk
import time


class App(Frame):
    def __init__(self):
        self.root = tk.Tk()

        self.frame = Frame.__init__(self, self.root)
        self.label = Label(text="", fg="Red", font=("Helvetica", 10))
        self.label.grid(row=6, column=1)
        Label(self.frame, text='Current time:').grid(row=6, column=0)
        self.root.geometry("300x250")
        self.root.wm_title("Jotnar controller_version 1.0 beta")

        Label(self.root, text='Percentage(%)').place(x=140, y=0)
        Label(self.root, text='1').place(x=10, y=30)
        Label(self.root, text='2').place(x=10, y=60)
        Label(self.root, text='3').place(x=10, y=90)
        Label(self.root, text='4').place(x=10, y=120)
        Label(self.root, text='5').place(x=10, y=150)
        Label(self.root, text='6').place(x=10, y=180)

        self.spin1 = Spinbox(self.root, from_=0, to=100, width=5)
        self.spin1.place(x=150, y=30)

        self.spin2 = Spinbox(self.root, from_=0, to=100, width=5)
        self.spin2.place(x=150, y=60)

        self.spin3 = Spinbox(self.root, from_=0, to=100, width=5)
        self.spin3.place(x=150, y=90)

        self.spin4 = Spinbox(self.root, from_=0, to=100, width=5)
        self.spin4.place(x=150, y=120)

        self.spin5 = Spinbox(self.root, from_=0, to=100, width=5)
        self.spin5.place(x=150, y=150)

        self.spin6 = Spinbox(self.root, from_=0, to=100, width=5)
        self.spin6.place(x=150, y=180)

        self.var1 = tk.StringVar()
        self.toggle = tk.Checkbutton(self.root, onvalue="ON", offvalue="OFF", width=4,
                                     indicatoron=False,
                                     variable=self.var1, textvariable=self.var1,
                                     selectcolor="green", background="red",
                                     command=self.toggle1)
        self.var1.set("OFF")
        self.toggle.place(x=220, y=30)

        self.var2 = tk.StringVar()
        self.toggle2 = tk.Checkbutton(self.root, onvalue="ON", offvalue="OFF", width=4,
                                      indicatoron=False,
                                      variable=self.var2, textvariable=self.var2,
                                      selectcolor="green", background="red",
                                      command=self.toggle2)
        self.var2.set("OFF")
        self.toggle2.place(x=220, y=60)

        self.var3 = tk.StringVar()
        self.toggle3 = tk.Checkbutton(self.root, onvalue="ON", offvalue="OFF", width=4,
                                      indicatoron=False,
                                      variable=self.var3, textvariable=self.var3,
                                      selectcolor="green", background="red",
                                      command=self.toggle3)
        self.var3.set("OFF")
        self.toggle3.place(x=220, y=90)

        self.var4 = tk.StringVar()
        self.toggle4 = tk.Checkbutton(self.root, onvalue="ON", offvalue="OFF", width=4,
                                      indicatoron=False,
                                      variable=self.var4, textvariable=self.var4,
                                      selectcolor="green", background="red",
                                      command=self.toggle4)
        self.var4.set("OFF")
        self.toggle4.place(x=220, y=120)

        self.var5 = tk.StringVar()
        self.toggle5 = tk.Checkbutton(self.root, onvalue="ON", offvalue="OFF", width=4,
                                      indicatoron=False,
                                      variable=self.var5, textvariable=self.var5,
                                      selectcolor="green", background="red",
                                      command=self.toggle5)
        self.var5.set("OFF")
        self.toggle5.place(x=220, y=150)

        self.var6 = tk.StringVar()
        self.toggle6 = tk.Checkbutton(self.root, onvalue="ON", offvalue="OFF", width=4,
                                      indicatoron=False,
                                      variable=self.var6, textvariable=self.var6,
                                      selectcolor="green", background="red",
                                      command=self.toggle6)
        self.var6.set("OFF")
        self.toggle6.place(x=220, y=180)

        self.update_clock()
        mainloop()

    def toggle1(self):
        if self.var1.get() == "ON":
            print('turning on: ' + self.spin1.get())
        else:
            print("turning off...")

    def toggle2(self):
        if self.var2.get() == "ON":
            print('turning on: ' + self.spin2.get())
        else:
            print("turning off...")

    def toggle3(self):
        if self.var3.get() == "ON":
            print('turning on: ' + self.spin3.get())
        else:
            print("turning off...")

    def toggle4(self):
        if self.var4.get() == "ON":
            print('turning on: ' + self.spin4.get())
        else:
            print("turning off...")

    def toggle5(self):
        if self.var5.get() == "ON":
            print('turning on: ' + self.spin5.get())
        else:
            print("turning off...")

    def toggle6(self):
        if self.var6.get() == "ON":
            print('turning on: ' + self.spin6.get())
        else:
            print("turning off...")

    def update_clock(self):
        now = time.strftime("%H:%M:%S")
        self.label.configure(text=now)
        self.after(1000, self.update_clock)


app = App()

Return to “Python”