read widget values in python Tkinter - python

i want to recover the real value entered by users to a variable using Tkinter.
Here is my code
from Tkinter import *
master = Tk()
Label(master, text="real value 1").grid(row=0)
Label(master, text="real value 2").grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
print e1.get()
x=e1.get()
mainloop( )
However my code can neither print the real value i have entred in the table nor assign this number to my variable x (a variable i would like to use outside tkinter). How can i get the value e1, e2 entered by users?

As has been mentioned, you are getting the value of the entry widgets before the user has time to enter any text. A simple way to deal with this is to read the values after a user action such as pressing a button.
from Tkinter import *
def readValues():
print(e1.get())
print(e2.get())
master = Tk()
Label(master, text="real value 1").grid(row=0)
Label(master, text="real value 2").grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
Button(master,text="Go",command=readValues).grid(row=2)
mainloop( )
You could also do this based on when the window is closed.

Related

Tkinter - Iterating default text entry

I'm trying to create a GUI which will enable me to create folders with iterating names each time I click a button, i.e. 'folder_1', 'folder_2, 'folder_3',... which is isn't too difficult.
But, I also want the ability to manually change the number at which the iteration starts: I can enter '10' in an entry box and click the button, which would create 'folder_10' and each time I click the button after that it would continue to iterate, i.e folder_11, folder_12, folder_13,...
It would also be very helpful if the entry box contained the number of the next folder, as a default (that I can change manually), to be created when I click the button.
I've been trying to get something like this to work but no luck:
from tkinter import *
master = Tk()
master.counter = 0
def create_folder():
newfoldername = 'Folder_'+e1.get()+'/'
master.counter += 1
print(newfoldername)
#...folder creation here (I know how to do this...)
e1 = Entry(master)
e1.insert(0, master.counter)
e1.grid(row=0, column=1)
Button(master, text='Create folder', command=create_folder).grid(row=1,
column=1)
mainloop()
I am newbie to python so this might be really simple...
Thanks.
I would recommend you use IntVar()(If you type string in the Entry and use .get(),you will get error.) or StringVar().
from tkinter import *
master = Tk()
master.counter = IntVar()
master.counter.set(0)
def create_folder():
newfoldername = 'Folder_'+str(master.counter.get())+'/'
master.counter.set(master.counter.get()+1)
print(newfoldername)
#...folder creation here (I know how to do this...)
e1 = Entry(master, textvariable=master.counter)
e1.grid(row=0, column=1)
Button(master, text='Create folder', command=create_folder).grid(row=1,
column=1)
mainloop()
Shows the next value in the Entry box and creates directory with that number. If you change the value in the Entry box, it continues from there.
from tkinter import *
master = Tk()
master.counter = 0
def create_folder():
newfoldername = 'Folder_'+e1.get()+'/'
master.counter = int(e1.get())
master.counter += 1
e1.delete(0, END)
e1.insert(0, str(master.counter))
print(newfoldername)
#...folder creation here (I know how to do this...)
e1 = Entry(master)
e1.insert(0, master.counter)
e1.grid(row=0, column=1)
Button(master, text='Create folder', command=create_folder).grid(row=1,
column=1)
mainloop()

Tkinter button not returning value from function

I am currently trying to create a small UI for a function that returns some value from a data frame based on the string that has been entered. The string is split and each individual substring is looked up in my data frame using iloc. The issue is, when calling this function using a button in Tkinter, nothing is being returned. It works fine without Tkinter so I am unsure where the error is occurring.
master = Tk()
e1 = Entry(master)
list_of_inputs = e1.get().split()
def show_entry_fields():
i=0
while (i<len(list_of_inputs)):
return (backend.loc[backend['Keyword'] == list_of_inputs[i]])
i=i+1
Label(master, text="Enter Title").grid(row=0)
e1.grid(row=0, column=1)
Button(master, text='Show', command=show_entry_fields).grid(row=0, column=2, sticky=W, pady=4)
mainloop( )
Couple of things:
You are checking the value of your Entry before the user has a chance to fill it in. You should get the user input only when the button is pressed.
Your while loop in the show_entry_fields() function will always run once regardless of how many inputs provided, since you put a return statement inside the loop
Your handler function has to modify an existing data structure or graphical component, if you return your result you cant collect it
Possible implementation:
master = Tk()
e1 = Entry(master)
def show_entry_fields():
list_of_inputs = e1.get().split()
result = []
for entry in list_of_inputs:
result.append(backend.loc[backend['Keyword']] == entry)
# Alternatively, you can create the list with
# result = [backend.loc[backend['Keyword']] == entry for entry in list_of_inputs]
# Here, deal with the result
Label(master, text="Enter Title").grid(row=0)
e1.grid(row=0, column=1)
Button(master, text='Show', command=show_entry_fields).grid(row=0, column=2, sticky=W, pady=4)
mainloop()

Returning value from function and inserting to Entry widget

Hey I'm building a simple pace calculator using Tkinter. I managed to get 2 values which are needed for my calculation and I want to return my calculation in the third Entry widget. I've got two functions that returns the get_value to console window. Next I want to do the calculations on get_value and get_value1 and insert into the third entry box.
Also can someone explain to me why in the return_min and return_sec functions need en parameter to work?
from tkinter import *
top = Tk()
top.title("Pace Calculator")
def return_min(en):
get_value = E1.get()
get_value = int(get_value)
get_value *= 2
print(get_value)
def return_sec(en):
get_value = E2.get()
get_value = int(get_value)
get_value *= 3
print(get_value)
L1 = Label(top, text="minutes")
L1.grid(row=0, column=0)
E1 = Entry(top, bd=5)
E1.grid(row=1, column=0)
E1.bind('<Return>', return_min)
L2 = Label(top, text=" seconds")
L2.grid(row=2, column=0)
E2 = Entry(top, bd=5)
E2.grid(row=3, column=0)
E2.bind('<Return>', return_sec)
L3 = Label(top, text = "Pace")
L3.grid(row = 4, column = 0)
E3 = Entry(top, bd=5)
E3.grid(row=5, column=0)
top.mainloop()
If you want to "insert" some value into your E3 tkinter.Entry, you can do this:
E3.delete(0, END)
E3.insert(0, your_value)
I must say that Entry widgets are meant for input data, not output! But I can tell you are just getting started and trying things out, so let me know if you need more help with this.

iPython: How to get 3 variables from user

I've got a problem. I have to write program, that check what was day of the week of given DAY, MONTH, YEAR.
I defined a function 'dayoftheweek' which finds day of the week of given date.
But here is my problem. I don't know how to get three variables d,m,y from user with GUI.
Also I don't know how to make a fine button saying "Accept" and will move my 3 variables into dayoftheweek function.
Here is the code
import math
from tkinter import *
def dayoftheweek(d, m, y):
a=math.floor((14-m)/12)
y1=r+4800-a
n=m+12*-3
l=d+math.floor((153*n)/5)+365*y1+math.floor(y1/4)-math.floor(y1/100)+math.floor(y1/100)-32045
p = (l%7)+1
return p
def date():
d = int(e1.get())
m = int(e2.get())
y = int(e3.get())
print(dayoftheweek(d, m, y))
master = Tk()
Label(master, text="Day").grid(row=0)
Label(master, text="Month").grid(row=1)
Label(master, text="Year").grid(row=2)
e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
mainloop( )
To print something in Tkinter, you can't just use print statement in your code.
You have to have a Label somewhere, and then change the contents of that Label. To do that you have to have a StringVar assigned to that Label. A StringVar is like a special case of normal strings, special because everytime it changes it will refresh how your window looks. That's what I did here:
printVar = StringVar()
Label(master, textvariable=printVar).grid(row=4, column=1)
Just added a Label in which we can update some text, aka "print to".
Additionally I added a Button, so that you could call your function date. You can make the window do something by using buttons that call assigned commands, or by binding keys (i.e. Enter, Space...) to the window.
button = Button(master, text="Ok", command=date).grid(row=3, column=1))
Now everytime you press your button the function date will be called, and the content of printVar will be changed and displayed in the Label.
Here's the full code, I also added the r=100 because your code is incomplete and gives the error that global r is not defined.
import math
from Tkinter import *
def dayoftheweek(d, m, y):
r = 100
a=math.floor((14-m)/12)
y1=r+4800-a
n=m+12*-3
l=d+math.floor((153*n)/5)+365*y1+math.floor(y1/4)-math.floor(y1/100)+math.floor(y1/100)-32045
p = (l%7)+1
return p
def date():
d = int(e1.get())
m = int(e2.get())
y = int(e3.get())
printVar.set(dayoftheweek(d, m, y))
master = Tk()
Label(master, text="Day").grid(row=0, column=0)
Label(master, text="Month").grid(row=1, column=0)
Label(master, text="Year").grid(row=2, column=0)
e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
printVar = StringVar()
Label(master, textvariable=printVar).grid(row=4, column=1)
button = Button(master, text="Ok", command=date).grid(row=3, column=1)
mainloop( )

Trying to change label using entry box and button

I'm trying to make a label that will change when I enter text into the entry box and click the button.
I've tried doing some research but can't seem to find out how to do it .
from tkinter import *
master = Tk()
master.title("Part 3")
v = StringVar()
v.set("Please change me")
lb= Label(master, textvariable=v, fg="red",bg="black").grid(row=0,column=0)
ent= Entry(master, textvariable=v,).grid(row=1,column=2)
b1= Button(master, text="Click to change", fg="red",bg="black").grid(row=1,column=0)
to do so, you first need to define a callback that changes the value. (example below)
You should also use two Variables of type StringVar to store the different Values
from tkinter import *
master = Tk()
master.title("Part 3")
lText = StringVar()
lText.set("Please change me")
eText = StringVar()
def ChangeLabelText(event=None):
global lText
global eText
lText.set(eText.get())
Then, bind the callback to the button
lb = Label(master, textvariable=lText, fg="red",bg="black").grid(row=0,column=0)
ent = Entry(master, textvariable=eText).grid(row=1,column=2)
b1 = Button(master, text="Click to change", fg="red",bg="black", command=ChangeLabelText).grid(row=1,column=0)

Categories

Resources