timothyy
Posts: 22
Joined: Thu Aug 08, 2019 9:20 am

How to using a value from spinbox

Thu Aug 08, 2019 11:28 am

I wanted to use the spinbox value to control the output percentage for my on/off control. I tried DACp(13,spinsee1) and DACp(14,spinsee2), it seems that python does not accept this type of input, so it must be a number, for example: DACp(14,100). Is there a way that i can change into number and be recognised by python?

Code: Select all

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

root.wm_title("Testing_version 1.0 beta") 
root.geometry("300x250")

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


def toggle1():
    if var1.get() == "ON":
        spinsee1=spin1.get()
        DACp(13,spinsee1)
    else:
        DACoff(13)

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":
        spinsee2=spin2.get()
        DACp(14,spinsee2)
    else:
        DACoff(14)

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)

mainloop()

Code for DACp & DACoff from pwmdac.py

Code: Select all

def DACoff():
    set_all_pwm(0, 4095)
    output_disable() 
            
            
def DACp(chn,perc):
    pulsewidth = int(4095/100*perc)
    DACon(chn,pulsewidth)

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

Thu Aug 08, 2019 2:13 pm

The value returned from spinbox is a string. To convert the string to an integer use int(). For example if the value returned is "4", and stored in a variable called x to convert :

Code: Select all

value = int(x)

timothyy
Posts: 22
Joined: Thu Aug 08, 2019 9:20 am

Re: How to using a value from spinbox

Thu Aug 08, 2019 2:49 pm

The code i have changed into:

Code: Select all

spin1 = Spinbox(root, from_=0, to=100, width=5)
global spinsee1
spin1.place(x=150, y=30)
spinsee1=spin1.get()
spinsee1= int(x)
The DACp should be:

Code: Select all

 DACp(13,x)

User avatar
joelostinspace
Posts: 14
Joined: Sat Aug 10, 2019 2:51 pm
Location: Earth

Re: How to using a value from spinbox

Sat Aug 10, 2019 3:05 pm

Timothy the example is not literal but a general way of converting a string to int- examine the use of classes
it eliminates the use of global variables.

Code: Select all

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

I would rather live in a world where my life is surrounded by mystery than a world so small that my mind could comprehend it.


--- Harry Emerson Fosdick

Return to “Python”