iPython: How to get 3 variables from user - python

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( )

Related

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()

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()

read widget values in python Tkinter

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.

How to get the value of the selected radio button?

I would like to create 2 different groups of radio buttons. The user would select one option from either group. There would be a function that would get the values(strings) from the selected radio buttons and then print them. Here's my code but it doesn't work (i'm new to python).
from tkinter import *
root = Tk()
btn1 = "lol"
btn2 = "lel"
def funkcija():
n = entry1.get()
m = "null"
X = btn1.get()
Y = btn2.get()
print("%s %s je %s %s." % (n, X, m, Y))
theLabel = Label(root, text="Vnesite količino in izberite prvo valuto.")
theLabel.grid(row=0, columnspan=3)
gumb1=Radiobutton(root,text="Euro",value = "euro",variable = "btn1").grid(row=2, column=1, sticky=W)
gumb2=Radiobutton(root,text="Dolar",value = "dolar",variable = "btn1").grid(row=3, column=1, sticky=W)
gumb3=Radiobutton(root,text="Funt",value = "funt",variable = "btn1").grid(row=4, column=1, sticky=W)
label3= Label(root, text="Izberite drugo valuto.")
label3.grid(row=6, columnspan=3)
label35= Label(root)
label35.grid(row=5, columnspan=3)
gumb4=Radiobutton(root,text="Euro",value = "euro",variable = "btn2").grid(row=7, column=1, sticky=W)
gumb5=Radiobutton(root,text="Dolar",value = "dolar",variable = "btn2").grid(row=8, column=1, sticky=W)
gumb6=Radiobutton(root,text="Funt",value = "funt",variable = "btn2").grid(row=9, column=1, sticky=W)
label1 = Label(root, text="Količina:")
label1.grid(row=1, sticky=E)
entry1 = Entry(root)
entry1.grid(row=1, column=1, sticky=W)
go = Button(root, text="Izračun", fg="white", bg="black", command=funkcija)
go.grid(row=10, columnspan=3)
root.mainloop()
In your radio button, analyze the parameters that you are passing:
gumb1 = Radiobutton(root,
text = "Euro",
value = "Euro",
variable = "btn2"
The parameters value and variable are what stores the data of the radio button. You've set your value option correctly. The interpreter will automatically set the variable with the value when the radio button is selected.
But here's where your issue is:
variable = "btn2"
"btn2" is a string. Not very useful though, is it? In fact, you're trying to perform methods on it that don't even exist. Such as here:
def funkcija():
X = btn2.get()
In fact, taking this information, you almost got there!
At the top of your script, you need to set btn2 to Tkinter's StringVar, like so:
from tkinter import *
btn1 = StringVar()
btn2 = StringVar()
Now that's done, let's change our parameters in our radio buttons.
gumb1 = Radiobutton(root,
text = "Euro",
value = "Euro",
variable = btn2
Now, Tkinter will automatically update the variable when it is selected. To get the value, do the same that you had done in your funkcija.
X = btn2.get()
And then the value of btn2 (which was updated by the radio buttons) will not be read, and stored into the variable X.

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