Tkinter .get() from entry gives weird values - python

I have a simple GUI program using Tinter and Turtle where the goal is to draw a polygon. I ask the user for the number of sides and length of side. I made a simple GUI, but when I click the button to get the values I get very small decimal digit values and I can't figure out why.
import Tkinter as tk
def draw_polygon():
num_of_sides.get()
length_of_sides.get()
print num_of_sides, length_of_sides
root = tk.Tk()
tk.Label(root, text='Number of Sides').grid(row=0)
tk.Label(root, text='Length of Sides').grid(row=1)
num_of_sides = tk.Entry(root)
num_of_sides.grid(row=0, column=1)
length_of_sides = tk.Entry(root)
length_of_sides.grid(row=1, column=1)
draw_button = tk.Button(root, text='Draw', command=draw_polygon)
draw_button.grid(row=3, column=1)
tk.mainloop()

You must assign the return value of get() to a variable. Otherwise, you print the name of the widget, which by default is a point followed by a number.
import Tkinter as tk
root = tk.Tk()
tk.Label(root, text='Number of Sides').grid(row=0)
tk.Label(root, text='Length of Sides').grid(row=1)
num_of_sides = tk.Entry(root, name = "numOfSides")
num_of_sides.grid(row=0, column=1)
length_of_sides = tk.Entry(root, name = "lenOfSides")
length_of_sides.grid(row=1, column=1)
def draw_polygon():
ns = num_of_sides.get()
ls = length_of_sides.get()
print ns, ls
print num_of_sides, length_of_sides
draw_button = tk.Button(root, text='Draw', command=draw_polygon)
draw_button.grid(row=3, column=1)
tk.mainloop()
Of course, you may comment or remove the second print, I left it for the explanation. You may also remove the two name= arguments.

Related

Extract text from Entry Box ktinker Python

I have a tkinter project and need the text, I put it in the Entry box, printed it in the console. but when I use get() I get empty space in the console.
this is my interface. when I put 1234 I could place it above when I click the button "Pull record data" but I need that number in my console. to use that entry for other things. but when I try to print it I get nothing in the console like this:
root = tk.Tk()
root.geometry("390x500")
root.title("Airtable Project Tracker")
str = tk.StringVar(root)
str.set('ie. recveGd8w9ukxLkk9')
tk.Label(root, textvariable=str).pack()
recid = tk.Entry(root, width=50)
recid.pack(pady = 10)
print(recid.get())
id_button = tk.Button(root, text="Pull record data", width=50, command = lambda:str.set(recid.get()))
id_button.pack(pady = 5)
As you have already implemented it above, you can get the input plane value using get() (recide.get()). Another method is to use the parameter textvariable as shown below. Where the value can be accessed again using get() (text.get()).
text = tk.StringVar()
recide = tk.Entry(root, width=50, textvariable=text)
In my test case I could not reproduce this error. I modified their code slightly to output the value in the console as shown below.
import tkinter as tk
def test():
str.set(recid.get())
print(recid.get())
root = tk.Tk()
str = tk.StringVar(root)
str.set('ie. recveGd8w9ukxLkk9')
tk.Label(root, textvariable=str).pack()
recid = tk.Entry(root, width=50)
recid.pack(pady = 10)
print(recid.get())
id_button = tk.Button(root, text="Pull record data", width=50, command = test)
id_button.pack(pady = 5)
root.mainloop()

One of my variables is being printed but the other is not in tkinter Entry boxes

I'm trying to create a function in tkinter where I can print out what the user writes in a Entry box. I'm able to print out ask_an_entry_get, but when I try to print what_is_answer_entry_get
, I get nothing my empty spaces.
Please find out the problem here. Also I'm using the Entry widget, along with the get() function, to get input from the user.
def answer_quizmaker_score():
print(ask_an_entry_get)
print(what_is_answer_entry_get)
I made a lot of global variables so I could use them all around my code.
global what_is_answer_entry
what_is_answer_entry = Entry(root4)
what_is_answer_entry.pack()
I then used the get() function to retrieve what the user typed.
global what_is_answer_entry_get
what_is_answer_entry_get = what_is_answer_entry.get()
This is the exact process I did for both ask_an_entry_get and what_is_answer_entry_get. However for some reason only ask_an_entry_get is printed, while what_is_answer_entry_get is printing nothing in the console.
from tkinter import *
root = Tk()
root.geometry("500x500")
txt1 = StringVar()
txt2 = StringVar()
def txt_printer():
print(txt1.get())
print(txt2.get())
x = Entry(root, textvariable=txt1, width=20)
x.place(x=0, y=0)
y = Entry(root, textvariable=txt2, width=20)
y.place(x=0, y=50)
btn_print = Button(root, text="print", command=txt_printer)
btn_print.place(x=0, y=100)
# Or if you want to show the txt on window then:
def txt_on_window():
lb1 = Label(root, text=txt1.get())
lb1.place(x=0, y=200)
lb2 = Label(root, text=txt2.get())
lb2.place(x=0, y=235)
btn_print_on_window = Button(root, text="print on screen", command=txt_on_window)
btn_print_on_window.place(x=0, y=150)
root.mainloop()

How can I append elements inputted by user in a tk.Entry object into a list?

I'm trying to build a very simple program in Python and Tkinter that allows the user to input people's names by keyboard and then a button is commanded to select a person from the list at random and show it in a tk.Label object.
My problem is once I run the root.mainloop(), I can add names to the list but the list does not update with the new names.
This is the main code for the Tkinter to initialize
import tkinter as tk
from tkinter import filedialog, Text
import random
root = tk.Tk()
root.title('Millor persona del moment')
root.geometry("500x200")
root.configure(bg='black')
peopleList = []
tk.Label(root, text="Insertar participante: ",fg="#ff007f", bg='black').grid(row=0)
e1 = tk.Entry(root)
e1.grid(row=0, column=1)
addButton = tk.Button(root, text='AƱadir', padx=10, pady=5, fg="#ff007f", bg='black', command=addPerson)
addButton.grid(row=0, column=2)
while peopleList:
turnButton = tk.Button(root, text='Saca Tema', padx=10, pady=5, fg="#ff007f", bg='black', command=wordTurn(peopleList))
turnButton.grid(row=1, column=0)
nom = tk.StringVar()
nom.set('Comencem')
personSpeaking = tk.Label(root, textvariable=nom,fg="#ff007f", bg='black')
personSpeaking.grid(row=1, column=1)
root.mainloop()
And these are the functions I use
def addPerson():
peopleList.append(e1.get())
e1.delete(0,'end')
def wordTurn(peopleList):
person = random.choice(peopleList)
peopleList.remove(person)
nom.set(person)
command=wordTurn(peopleList)) calls the return value of wordTurn when the button is pressed, which is None and should raise an error. Use command=lambda peopleList=peopleList: wordTurn(peopleList)) instead.

how to create an entry that begins with a dollar sign in python tkinter

how can i create an entry that begins with a dollar sign so i don't have to enter the sign every time into the entry in python tkinter here is the code
price = Entry(Forms, textvariable=PRICE, width=30)
price.grid(row=1, column=1)
You can set text in PRICE at start
PRICE = tk.StringVar(value='$')
or
PRICE.set('$')
and then Entry(..., textvariable=PRICE) will display it at start. But user can delete it and would need more work to block it.
import tkinter as tk
root = tk.Tk()
PRICE = tk.StringVar(value='$')
#PRICE.set('$')
entry = tk.Entry(root, textvariable=PRICE)
entry.grid(row=0, column=1)
root.mainloop()
I would rather put $ as Label on the left of Entry and then user can write only number.
import tkinter as tk
root = tk.Tk()
PRICE = tk.StringVar()
label = tk.Label(root, text='$')
label.grid(row=0, column=0)
entry = tk.Entry(root, textvariable=PRICE)
entry.grid(row=0, column=1)
root.mainloop()

How to correct the output from an entry box

When using the feeder button, the script for F runs through entirely through to the print before the 'master' box appears, then does not react to the inputs from the 'master' box. This results in the output being 0.0 kW because the input is a long decimals followed by an L, when what I, the user inputs is 8777
I have been roaming the internet for about a day now with no luck finding anything. I am very new to TK but have been trying to learn it.
def F():
master = tk.Tk()
tk.Label(master, text = 'Feeder Number: ').grid(row=0)
entry1 = tk.Entry(master)
entry1.grid(row=0, column=1)
button2 = tk.Button(master,
text=' Confirm',
command=entry1.get())
button2.pack()
button2.grid(row=0, column=2)
fn = entry1.pack()
print fn
feed = filtered['Feeder']==fn
feedfn = filtered[feed]
Cap = feedfn['AC Name Plate Capacity <= 10kw']
Cap = Cap.astype(float)
AcPv = Cap.sum()
print 'The total PV on this feeder is:', AcPv, 'kW'
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
button = tk.Button(frame,
text='Exit',
fg='red',
command=quit)
button.pack()
button.grid(row=1, column=1)
Fee = tk.Button(frame,
text='Feeder',
command=F)
Fee.pack()
Fee.grid(row=0, column=1)
root.mainloop()
Expected 27.702
Output 0.0
Given that I will not be posting the csv,
entry1/fn should be 8777
currently 'none'
UPDATE
I am now receiving an output of PY_VAR when printing fn, I understand that the code is running all the way through before taking an input. Any recommendations for how to take the input before the filters are run?
def F():
master = tk.Tk()
tk.Label(master, text = 'Feeder Number: ').grid(row=0)
entry1 = tk.Entry(master)
entry1.grid(row=0, column=1)
button2 = tk.Button(master,
text=' Confirm',
command=entry1.get())
button2.grid(row=0, column=2)
fn = tk.IntVar()
print fn
feed = filtered['Feeder']==fn
feedfn = filtered[feed]
Cap = feedfn['AC Name Plate Capacity <= 10kw']
Cap = Cap.astype(float)
AcPv = Cap.sum()
print 'The total PV on this feeder is:', AcPv, 'kW'
For those interested in the final code (Which worked for me):
def F():
master = tk.Tk()
tk.Label(master, text = 'Feeder Number: ').grid(row=0)
entry = tk.Entry(master)
entry.grid(row=0, column=1)
def pint():
data = entry.get()
master.destroy()
feed = filtered['Feeder']==data
feedfn = filtered[feed]
Cap = feedfn['AC Name Plate Capacity <= 10kw']
Cap = Cap.astype(float)
AcPv = Cap.sum()
fdf = tk.Tk()
tk.Label(fdf, text = AcPv).grid(row=0)
button4 = tk.Button(fdf,
text = ' Exit',
fg='red',
command=fdf.destroy)
button4.grid(row=1)
button2 = tk.Button(master,
text=' Confirm',
command = pint)
button2.grid(row=0, column=2)
button3 = tk.Button(master,
text = ' Exit',
fg='red',
command=master.destroy)
button3.grid(row=0, column=3)
master.mainloop()
There a few mistake in your code that lead to the different output you have received.
First, why is your code executing without showing the master box :
Your tkinter need a mainloop() call if you want a persistent window.
master.mainloop()
You did that right with your root, but your master lacks that mainloop. This line is what basically keeping your GUI alive and looping over it for changes until it is destroyed one way or another. You need to add this line after creating your widgets in order to be able to interact with the window. Anything written after this line (but still indented in the definition) will be executed when your window is closed, either manually or with the command :
master.destroy()
Next, although this will yield a working window, you can still interact with your root window while the master window is up, which can lead to problems if you are expecting variable from master. I suggest you read about the Toplevel widget which is made specifically for cases like yours. (http://effbot.org/tkinterbook/toplevel.htm) Alternatively, you could also use tkinter's tkSimpleDialog.askinteger or askfloat functions which seems perfect for your application.
Finally, the entry allows you to write text but how to access the text? you can use Entry1.get() to extract the content of the entry, or as you have started to to in your update, you can assign a tkinter variable to the entry. This variable will be updated as you change write strings or numbers in the entry. To bind the variable to your entry, you must state it in the entry's creation :
fn = tk.StringVar(value = '000')
entry1 = tk.Entry(master, textvariable = fn)
*Note, this will require your fn variable to be initialized before the entry. Also, you can initialize a value for that variable upon creation
the tkinter variable is an object which is why when you print it, you get PY_VAR. (the type of object) To access the value, you need to use the get() method :
print(fn.get())

Categories

Resources