I was writing a code in Python (with Tkinter), and I couldn't fix a little problem. This is my code:
from tkinter import *
def show_entry_fields():
straal = int(e1.get())
area = 3.143 * (straal * straal)
area_label['text'] = str(area)
master = Tk()
Label(master, text="Straal").grid(row=0)
e1 = Entry(master)
e1.grid(row=0, column=1)
area_label = Label(master, text="Oppervlakte")
area_label.grid(row=2)
Button(master, text='Sluit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Bereken oppervlakte', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
mainloop()
When I run the program, then it does work, but if it calculates the area, then the text 'oppervlakte' gets replaced by the answer. Before you calculate:
http://imgur.com/zw7wA6D
After you calculate: http://imgur.com/RV94QTw
How can I let the GUI display:
Oppervlakte: 78.5749999999
Thanks in advance
You are replacing your label in the function you have: area_label['text'] = str(area) instead of creating a new label, create a new label with no text and then populate it in your function:
from tkinter import *
def show_entry_fields():
straal = int(e1.get())
area = 3.143 * (straal * straal)
calculation_label['text'] = str(area) #using new label
master = Tk()
Label(master, text="Straal").grid(row=0)
e1 = Entry(master)
e1.grid(row=0, column=1)
area_label = Label(master, text="Oppervlakte")
area_label.grid(row=2)
calculation_label = Label(master, text="") #new label
calculation_label.grid(row=2, column=1)
Button(master, text='Sluit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Bereken oppervlakte', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
mainloop()
You could change this:
area_label['text'] = str(area)
to this:
area_label['text'] = "Oppervlakte: {}".format(area)
If you don't want to duplicate the "Oppervlakte" text, you'll need to create two labels, one for "Oppervlakte" and another one (perhaps initially empty) to hold the actual number.
Related
(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)
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()
from tkinter import *
root = Tk()
fN = StringVar()
sN = StringVar()
age = StringVar()
yG = StringVar()
Label(root, text="First Name").grid(row=0, sticky=W, padx=4)
fName = Entry(root, width=50, textvariable=fN ).grid(row=0, column=1, sticky=E, pady=4)
Label(root, text="Surname").grid(row=1, sticky=W, padx=4)
sName = Entry(root, width=50, textvariable=sN).grid(row=1, column=1, sticky=E, pady=4)
Label(root, text="Age").grid(row=2, sticky=W, padx=4)
age = Entry(root, width=50, textvariable=age).grid(row=2, column=1, sticky=E, pady=4)
Label(root, text="Year Group").grid(row=3, sticky=W, padx=4)
yearGruop = Entry(root, width=50, textvariable=yG).grid(row=3, column=1, sticky=E, pady=4)
fName_1 = fN.get()
returning the value of the StringVar 'fN' and storing it
def print_():
print (fName_1)
not printing the contents of the first name entry box ##
Button(root, text="Create account", command=print_).grid(row=4, column=1)
root.mainloop()
Right now you are getting the contents as soon as you started the program thus you are getting empty value.
You need to get the value after clicking the button which means you should put the code that gets value inside of your method.
def print_():
fName_1 = fN.get()
print (fName_1)
Also, in your code fName, sName etc. are all set to None since grid() returns None. If you want to use them later you need to use grid on separate line.
fName = Entry(root, width=50, textvariable=fN )
fName.grid(row=0, column=1, sticky=E, pady=4)
Another point is, you don't need those stringvar values in your code either. You can directly get contents of Entry using get.
fName = Entry(root, width=50, textvariable=fN )
fName.grid(row=0, column=1, sticky=E, pady=4)
def print_():
print (fName.get())
Full disclosure, I am a Python newb, and even newer to Tkinter. I have the below code which I was able to parse together from various sources which shows the buttons and dialogue box as I want them to be shown. The "Cancel" button works appropriately, but when I enter proper credentials and click "OK", nothing happens. Based on what I have read, I think I may have a binding and/or callback issue, but I am not sure. After several hours of reading and watching YouTube videos, I am banging my head against the desk. Any help would be greatly appreciated.
from Tkinter import *
master = Tk()
def login_info():
bankUsername = bank_user.get()
bankPassword = bank_pass.get()
return
Label(master, text=str(properBankName) + " Username: ").grid(row=0, sticky = "E")
Label(master, text="Password: ").grid(row=1, sticky = "E")
master.title("Please Enter Credentials")
bank_user = Entry(master)
bank_pass = Entry(master)
bank_user.grid(row=0, column=1)
bank_pass.grid(row=1, column=1)
Button(master, height=1, width=8, text='OK', command=login_info).grid(row=3, column=0, sticky = "E", pady=4)
Button(master, height=1, width=8, text='Cancel', command=master.quit).grid(row=3, column=1, sticky = "W", pady=4)
master.mainloop()
I use global variables to keep values and then I use master.destroy() to close window.
(on Linux master.quit() doesn't execute master.destroy() which close window)
I use variable login to recognize which button was clicked.
I use columnspan=2 for Entry and move Buttons one cell to the right - and now it looks better.
BTW: lines Button(...).grid(...) were very long so I splited them into two lines to make more readable.
Code:
from Tkinter import *
# --- functions ---
def login_info():
# inform function to use external/global variables
global bankUsername
global bankPassword
global login
login = True
bankUsername = bank_user.get()
bankPassword = bank_pass.get()
# quit window
master.destroy()
# --- main ---
# create global variables
bankUsername = None
bankPassword = None
login = False
# - GUI
master = Tk()
Label(master, text="Username:").grid(row=0, sticky="E")
Label(master, text="Password:").grid(row=1, sticky="E")
master.title("Please Enter Credentials")
bank_user = Entry(master)
bank_pass = Entry(master)
bank_user.grid(row=0, column=1, columnspan=2)
bank_pass.grid(row=1, column=1, columnspan=2)
b = Button(master, height=1, width=8, text='OK', command=login_info)
b.grid(row=3, column=1, sticky="E", pady=4)
b = Button(master, height=1, width=8, text='Cancel', command=master.destroy)
b.grid(row=3, column=2, sticky="W", pady=4)
master.mainloop()
# --- executed after closing window ---
if login: # if login is True:
print(bankUsername)
print(bankPassword)
else:
print("Canceled")
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()