#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0

    Python functions help.


    So i am making a slider that changes the value automatically.
    so i have code that you can move the slider but it only prints the value at the very start. I want it to repeat the print function every second or half a second.

    I'm assuming i need the print in a seperate function but when i put it at the top the w.get() hasnt been defined. Im kinda confused.


    This is my current code:

    Code:
    from Tkinter import *
    import time
    
    root = Tk()
    
    w = Scale(root, from_=0, to=100)
    w.pack()
    
    print w.get()
    
    root.mainloop()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,929
    Rep Power
    481
    Code:
    # online tkinter reference
    # http://effbot.org/tkinterbook/radiobutton.htm
    # (I found the information John Grayson's Python and Tkinter Programming.)
    
    import Tkinter  # from module import * is nasty.
    
    root = Tkinter.Tk()
    
    w = Tkinter.Scale(root, from_=0, to=100)
    w.pack()
    
    def show_slider(root,w):
        millisecond = 1                              # (to show the units)
        print(w.get())
        root.after(1000*millisecond,show_slider,root,w)   # must re-install itself
    
    show_slider(root,w)
    
    root.mainloop()
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Thank you! I am actually using pyserial to write it to the serial port. I thought it was going to be as easy as changing print(wget()) to the serial function but its not. Any ideas?
    Otherwise thank you anyway
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,929
    Rep Power
    481
    I have no experience with pyserial.
    Last edited by b49P23TIvg; December 10th, 2012 at 02:35 PM. Reason: Not a command!
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    11
    Rep Power
    0
    Ok thanks anyway
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    615
    Rep Power
    65
    Another example:
    Code:
    ''' tk_scale102.py
    explore Tkinter's Scale (slider) widget response
    '''
    
    # for Python3 replace Tkinter with tkinter
    import Tkinter as tk  
    
    def show_position(value):
        """show slider position in the window title"""
        # you can use value which gives <type 'str'> or
        # scale.get() gives <type 'int'> 
        s = "position = %s" % scale.get()
        root.title(s)
    
    
    root = tk.Tk()
    # use width x height + x_offset + y_offset (no spaces!)
    root.geometry("350x100+30+30")
    
    # create a horizontal scale/slider with values 
    # from 0 to 100 sliding in steps of 1
    scale = tk.Scale(root,
                     label="move slider:", 
                     from_=0,
                     to=100,
                     resolution=1,
                     command=show_position,
                     orient='horizontal',
                     length=300)
    scale.pack(side='left', expand='yes', fill='y')
    
    root.mainloop()
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo