Python Forum
Help pulling tkinter Entry string data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help pulling tkinter Entry string data
#1
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)
Reply
#2
The variables must be declared as tkinter.StringVar()
see: http://infohost.nmt.edu/tcc/help/pubs/tk...ables.html
Reply
#3
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'
Reply
#4
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())
Reply
#5
self is not defined
Reply
#6
Not within the confines of a class, looking into that now.
Reply
#7
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.
Reply
#8
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.
Reply
#9
looks like the variable cycle is not an integer.
Just before that statement, place a:
print('type: {}, value: {}'.format(type(cycle), cycle)))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to open a popup window in tkinter with entry,label and button lunacy90 1 857 Sep-01-2023, 12:07 AM
Last Post: lunacy90
  Pulling Specifics Words/Numbers from String bigpapa 2 749 May-01-2023, 07:22 PM
Last Post: bigpapa
  Having trouble installing scikit-learn via VSC and pulling my hair out pythonturtle 1 749 Feb-07-2023, 02:23 AM
Last Post: Larz60+
  Tkinter Entry size limit vman44 3 6,610 Dec-22-2022, 06:58 AM
Last Post: vman44
  (Python) Pulling data from UA Google Analytics with more than 100k rows into csv. Stockers 0 1,209 Dec-19-2022, 11:11 PM
Last Post: Stockers
  Pulling username from Tuple pajd 21 3,328 Oct-07-2022, 01:33 PM
Last Post: pajd
  Auto increament in Entry field. in tkinter GUI cybertooth 7 4,063 Sep-17-2021, 07:56 AM
Last Post: cybertooth
  pulling multiple lines from a txt IceJJFish69 3 2,564 Apr-26-2021, 05:56 PM
Last Post: snippsat
  data from multiple Entry widgets beevee 13 4,409 Aug-16-2020, 12:31 PM
Last Post: deanhystad
  Pulling Information Out of Dictionary Griever 4 2,876 Aug-12-2020, 02:34 PM
Last Post: Griever

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020