Python Forum

Full Version: Help pulling tkinter Entry string data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I have just picked up python as of three days ago and I am in the process of making a program. This is also my first experience with programming so bare with me. I have spent an UNHEALTHY amount of time trying to learn and so far I have been able to crawl my way forward but I am afraid I'm stuck.

I am in the process of making a GUI to control a GPIO function I created that just makes a couple lights blink. For the time being it just makes two LED's blink simultaneously with a user inputted delay and a user inputted number of loops using tkinter's Entry widget. This is where my problem arises, when I try to pull the inputs from the Entry widget's I get a 'str' error, which makes sense because I assume the entry is of the string data type and my blink function must be looking for an integer data type? However, if I force this to be an integer I then get an "invalid for literal int() base 10" error prompt. Please help. 

Here is my current script: 

Disclaimer: There may be some syntax errors. The firewall at Deere doesn't allow me to connect to the network so I had to type all of this again.
import tkinter as tk
root=tk.Tk()
root.title("LED Controller")
import sys
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(11,GPIO.OUT)

CycleLabel = tk.Label(root, text="Cycles", font="Verdana 12 bold")
CycleLabel.grid(row=1,column=1)
CycleInput = tk.Entry(root)
CycleInput.grid(row=1,column=2)
CycleInput.focus_set()
cycle=CycleInput.get()

SpeedLabel = tk.Label(root, text="Speed", font="Verdana 12 bold")
SpeedLabel.grid(row=1,column=1)
SpeedInput = tk.Entry(root)
SpeedInput.grid(row=2,column=2)
SpeedInput.focus_set()
speed = SpeedInput.get()

def callback():
    print (CycleInput.get())
    print (SpeedInput.get())

def Blink(cycle,speed):
    for platypus in range (0, cycle):           ** This is the line that the error always points to. 
        print("Loop" + str(platypus+1))
        GPIO.output(7,True)
        GPIO.output(11, True)
        time.sleep(speed)
        GPIO.output(7, False)
        GPIO.output(11, False)
        time.sleep(speed)
        print("Done")

ButtonFrame = tk.Frame(root)
ButtonFrame.grid(row=3,column=1,columnspan=3)

B2 = tk.Button(ButtonFrame, text="Start", width=10, command=lambda:Blink(cycle,speed), font="Verdana 10 bold")
B2.grid(row=3,column=2)

B1 = tk.Button(ButtonFrame, text"Print Inputs", width=10, command=callback, font="Verdana 10 bold")
B1.grid(row=3,column=1)

def clear():
    CycleInput.delete(0, 'end')
    SpeedInput.delete(0, 'end')

B3 = tk.Button(ButtonFrame, text="Clear", width=10, command=clear, font="Verdana 10 bold")
B3.grid(row=3,column=3)

CloseFrame = tk.Frame(root)
CloseFrame.grid(row=4, column=1, columnsapn=3)

B4 = tk.Button(CloseFrame, text="Close", width=20, command=root.destroy, font="Verdana 10 bold", activebackground='red')
B4.grid(row=4,column=2)
The variables must be declared as tkinter.StringVar()
see: http://infohost.nmt.edu/tcc/help/pubs/tk...ables.html
I set a textvariable in the Entry widget and defined the variable as a StringVar but when I try to 'get' that value I am prompted with the error "get() missing 1 required positional argument: 'self' "

Such as:

y = tk.StringVar()
SpeedInput = tk.Entry(root, textvariable = y)
speed = int(y.get())
Error:
TypeError: get() missing 1 required positional argument 'self'
If y is within the confines of a class, and the StringVar is external from the method that it is used in,
both the definition of the variable and the use of same must reference self.
self.y = tk.StringVar()
SpeedInput = tk.Entry(root, textvariable = self.y)
speed = int(self.y.get())
self is not defined
Not within the confines of a class, looking into that now.
Please post the entire traceback when showing errors, it contains
much needed information important things like line number causing
the problem, program flow just before failure, etc.
When you see an error that has 'self' as part of the error message
I'm quite sure it has to be class related.
Traceback (most recent call last)
File "/usr/lib/python3.4/tkinter/__init__.py" . line 1536. in __call__
return self.funk(*args)
File "/home/pi/desktop/LED Controler.py". line 56. in <lambde>
82= tk.Button(ButtonFrame. text"Start" , width=10 , command=lambda: Blink(cycle,speed), font "Verdana 10 bold", activebackground= 'green')
File "/home/pi/desktop/LED Controler.py". line 40. in Blank
for x in range (0,cycle):
TypeError: 'str' object cannot be interpreted as an integer

This is back to the original problem at hand. For some reason when I force the variable 'speed' or 'cycle' to be an integer I just get another error. I will post the self error once I get it back to that point. I am leaving work now so it might take me a minute to respond.
looks like the variable cycle is not an integer.
Just before that statement, place a:
print('type: {}, value: {}'.format(type(cycle), cycle)))