(Edit: Final working code below!)
I have been trying to do a tkinter text box with three inputs at the beginning of a code.
My main objective is to retrieve these three inputs as strings, which I would be able to integrate with the rest of the code (anything that goes beyond the root.mainloop()).
I am able to to the tkinter basics, but I am unable to integrate the user response with the other part of the code.
import tkinter as tk
from tkinter import ttk
def close_window():
root.destroy()
def show_entry_fields():
print("First %s\nSecond %s\nThird %s" % (e1.get(), e2.get(), e3.get()))
firstinput = e1.get()
secondinput = e2.get()
thirdinput = e3.get()
root = tk.Tk()
tk.Label(root, text="First input").grid(row=0)
tk.Label(root, text="Second input").grid(row=1)
tk.Label(root, text="Third input").grid(row=2)
text1 = tk.StringVar()
text2 = tk.StringVar()
text3 = tk.StringVar()
e1 = tk.Entry(root, textvariable=text1)
e1.grid(row=0, column=1)
e2 = tk.Entry(root, textvariable=text2)
e2.grid(row=1, column=1)
e3 = tk.Entry(root, textvariable=text3)
e3.grid(row=2, column=1)
tk.Button(root, text='Enter', command=show_entry_fields).grid(row=3,
column=1,
sticky=tk.W,
pady=4)
tk.Button(root, text='Close', command=close_window).grid(row=4,
column=1,
sticky=tk.W,
pady=4)
root.mainloop()
firstinput
print(firstinput)
In that sense, my objective is to be able to use the firstinput, secondinput and thirdinput as the code goes on beyond the root.mainloop().
However, when I run this code, I get the following error:
NameError Traceback (most recent call last)
<ipython-input-1-76b7cc4daea7> in <module>
42 root.mainloop()
43
---> 44 firstinput
NameError: name 'firstinput' is not defined
If I can't callback upon those strings I defined in def show_entry_fields(), how can I use them in the remaining of the code?
I'm sorry if this sounded confusing, I'll be happy to better/further explain myself if necessary.
If someone can shed a light upon this, I'll be ever so grateful. Many thanks.
EDIT: With the help of the comments, here is the final (and working) version:
import tkinter as tk
from tkinter import ttk
def close_window():
root.destroy()
def show_entry_fields():
print("First %s\nSecond %s\nThird %s" % (e1.get(), e2.get(), e3.get()))
root = tk.Tk()
tk.Label(root, text="First input").grid(row=0)
tk.Label(root, text="Second input").grid(row=1)
tk.Label(root, text="Third input").grid(row=2)
text1 = tk.StringVar()
text2 = tk.StringVar()
text3 = tk.StringVar()
e1 = tk.Entry(root, textvariable=text1)
e1.grid(row=0, column=1)
e2 = tk.Entry(root, textvariable=text2)
e2.grid(row=1, column=1)
e3 = tk.Entry(root, textvariable=text3)
e3.grid(row=2, column=1)
tk.Button(root, text='Enter', command=show_entry_fields).grid(row=3,
column=1,
sticky=tk.W,
pady=4)
tk.Button(root, text='Close', command=close_window).grid(row=4,
column=1,
sticky=tk.W,
pady=4)
root.mainloop()
firstinput = text1.get()
secondinput = text2.get()
thirdinput = text3.get()
print(firstinput)
Related
I want to take user input and output it inside GUI ...
my code
from tkinter import *
root = Tk()
root.geometry("644x344")
def printSomething():
label = Label(root, text="???")
label.grid(row=1, column=2)
ok=Label(root, text="Type your name").grid(row=2,column=1)
entryvalue = StringVar()
entry= Entry(root, textvariable=entryvalue)
entry.grid(row=2, column=2)
button = Button(root, text="Print Me", command=printSomething)
button.grid(row=3, column=2)
root.mainloop()
To get the text from an input box, use inputbox.get(). Also, don't set the ok variable to Label(root, text="Type your name").grid(row=2,column=1). This will be set as NoneType, so do
ok = Label(root, text="Type your name").grid(row=2,column=1)
ok.grid(row=2, column=1)
Here is your code:
from tkinter import *
root = Tk()
root.geometry("644x344")
def printSomething():
label = Label(root, text=entry.get())
label.grid(row=1, column=2)
ok=Label(root, text="Type your name")
ok.grid(row=2,column=1)
entryvalue = StringVar()
entry= Entry(root, textvariable=entryvalue)
entry.grid(row=2, column=2)
button = Button(root, text="Print Me", command=printSomething)
button.grid(row=3, column=2)
root.mainloop()
First thing, In order to accept and display the output on the screen you have to use either Label widget or Canvas Text. Since your code is not updating the Label widget thus I am here doing what you want to do.
First, create a Label widget in the main window,
Get the user input by using.get() method,
print and display the user input.
from tkinter import *
root = Tk()
root.geometry("644x344")
def printSomething():
label.config(text=entry.get())
ok=Label(root, text="Type your name")
ok.grid(row=2,column=1)
entryvalue = StringVar()
entry= Entry(root, textvariable=entryvalue)
entry.grid(row=2, column=2)
button = Button(root, text="Print Me", command=printSomething)
button.grid(row=3, column=2)
#Create a Label to print the Name
label= Label(root, text="", font= ('Helvetica 14 bold'), foreground= "red3")
label.grid(row=1, column=2)
root.mainloop()
I'm trying to get the value of an entry in tkinter!
when I print the value directly ( for example print(e2.get() ) it works well, but when I put this into a variable and then print it ( for example x = e2.get() print(x) ) it's not working!
here is the code:
import tkinter as tk
def show_entry_fields():
print("First Name: %s\n Last Name: %s" % (x, e2.get()))
master = tk.Tk()
tk.Label(master,
text="First Name").grid(row=0)
tk.Label(master,
text="Last Name").grid(row=1)
e1 = tk.Entry(master)
e2 = tk.Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
x = e1.get()
tk.Button(master,
text='Quit',
command=master.quit).grid(row=3,
column=0,
sticky=tk.W,
pady=4)
tk.Button(master,
text='Show', command=show_entry_fields).grid(row=3,
column=1,
sticky=tk.W,
pady=4)
tk.mainloop()
I need to put the entry value in a variable, how can I do it?
It's simple if you understand :
When the code pass by x = e1.get(), e1 equals just "".
After your write in e1 and call the def show_entry_fields when you click in the button.
But x is always equals to "".
And e1.get() get the text in the e1.
So it's why print(x) equals "" because you pass by the line x = e1.get() just one time !
I've looked other questions and have tried to fix this but I am really struggling with having my code print back the answer.
from tkinter import *
root = Tk()
label1 = Label(root, text="Number 1")
label2 = Label(root, text="Number 2")
labelplus = Label(root, text="+")
label1.grid(row=0, sticky=E)
label2.grid(row=3, sticky=E)
labelplus.grid(row=2, column=1)
entry1_var= StringVar()
entry2_var= StringVar()
entry1 = Entry(root, textvariable= entry1_var)
entry2 = Entry(root, textvariable= entry2_var)
entry1.grid(row=0, column=1)
entry2.grid(row=3, column=1)
first = (entry1_var.get()
second =(entry2_var.get()
def additionStuff(event):
totalNumbers = (first + second)
print(totalNumbers)
button1 = Button(root, text="Add Numbers")
button1.bind("<Button-1>", additionStuff)
button1.grid(row=4, column=1)
root.mainloop()
Why does my function now print back the answer?
You need to call StringVar.get inside of additionStuff:
def additionStuff(event):
first = entry1_var.get()
second = entry2_var.get()
totalNumbers = (float(first) + float(second))
print(totalNumbers)
Otherwise you're getting the value of first and second before the user has had opportunity to enter anything.
I made this GUI:
And after I click 'Run', my main program starts.
In my main program, I have a lot of prints that I want to display in the GUI, but have no idea how.
I saw some examples in google but it was really hard to understand and how to convert it to my needs, so: How to make a window with all the prints after I click the Run button ?
code:
from tkinter import *
from main import *
root = Tk()
root.configure(background="orange")
root.wm_title("Python Project")
label_1 = Label(root, text="Project Name",bg="orange",fg="black")
label_2 = Label(root, text="Site URL Link",bg="orange",fg="black")
entry_1 = Entry(root)
entry_2 = Entry(root)
label_1.grid(row=0,sticky=W)
label_2.grid(row=3333,sticky=W)
entry_1.grid(row=0, column=1, padx=50, ipadx=100)
entry_2.grid(row=3333, column=1, ipadx=100)
def callback():
a1 = entry_1.get()
a2 = entry_2.get()
mmm(a1,a2) # main program
button1 = Button(root,text="Run",command=callback)
button2 = Button(root,text="Quit",command=root.quit)
button1.grid(row=3334, ipadx=15, padx=50, column=1)
button2.grid(row=3335, column=1, ipadx=15, padx=50)
root.mainloop()
I wrote this piece of code, which is supposed to ask a user if all of his files have the same date and if yes, that he should write his date into a grid.
After entering the dates both windows should disappear and i want to keep the date.
Unfortunately I don't manage to let the first Input-Box disappear and after this whole procedure the entry date is [ ] again.
from tkinter import *
entry_date = []
if amountfiles == 1:
def moredates():
master.destroy()
def setdate():
def entry_date():
entry_date = e1.get()
entry_date = str(entry_date)
print("Date for all files is: ",entry_date)
master.destroy()
def quit():
sys.exit()
master = Tk()
Label(master, text="Please enter date (format YYYYMMDD, i.e. 20160824): ").grid(row=0)
e1 = Entry(master)
e1.grid(row=0, column=1)
Button(master, text='Quit', command=master.destroy).grid(row=3, column=1, sticky=W, pady=4)
Button(master, text='Insert', command=entry_date).grid(row=2, column=1, sticky=W, pady=4)
mainloop( )
master = Tk()
Label(master, text="Do all files have the same date?").grid(row=0)
Button(master, text='No...', command=moredates).grid(row=2, column=0, sticky=W, pady=4)
Button(master, text='Yes!', command=setdate).grid(row=1, column=0, sticky=W, pady=4)
Button(master, text='Close & Contiune', command=master.destroy).grid(row=3, column=0, sticky=W, pady=4)
mainloop( )
As the outer master variable is re-assigned in the function setdate(), the call master.destroy() will only close the new master, not the outer master. Try modifying the function setdate() as below:
def setdate():
def append_date():
date = e1.get() # get the input entry date
entry_date.append(date) # save the input date
print("Date for all files is: ", date)
master.destroy()
top = Toplevel() # use Toplevel() instead of Tk()
Label(top, text="Please enter date (format YYYYMMDD, i.e. 20160824): ").grid(row=0)
e1 = Entry(top)
e1.grid(row=0, column=1)
Button(top, text='Quit', command=master.destroy).grid(row=3, column=1, sticky=W, pady=4)
Button(top, text='Insert', command=append_date).grid(row=2, column=1, sticky=W, pady=4)
master.wait_window(top) # use Tk.wait_window()