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()
Related
from tkinter import *
def tell_story(Male):
story= "My name is" + "Male"
Frame.story_txt.delete(0.0, END)
Frame.story_txt.insert(0.0, story)
root = Tk()
root.title("game")
root.geometry("800x400")
Application = Frame(root)
Application.grid()
Lbl1 = Label(Application, text="story").grid(row=0, column=0, sticky=W)
Lbl2 = Label(Application, text="Male name").grid(row=1, column=0, sticky=W)
Male = Entry(Application).grid(row=1, column=1, sticky=W)
Button(Application, text="Click for story", command=tell_story).grid(row=11, column=0, sticky=W)
Frame.story_txt = Text(Application, width=75, height=10, wrap=WORD)
Frame.story_txt.grid(row=12, column=0, columnspan=4)
root.mainloop()
I'm unable to print story variable in text widget
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 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.
I have a very basic program that spits out a string of values, but I am not quite sure how to clear these values. At the moment I have it set up so that I just exit the window and start a new one so that I'm not rewriting over new values all the time. Is there a simple way to add another button that just says something like 'clear' and does exactly that? My code is as below:
def create_widgets(self):
self.entryLabel = Label(self, text="Please enter a list of numbers:")
self.entryLabel.grid(row=0, column=0, columnspan=2)
self.listEntry = Entry(self)
self.listEntry.grid(row=0, column=2, sticky=E)
self.entryLabel = Label(self, text="Please enter an index value:")
self.entryLabel.grid(row=1, column=0, columnspan=2, sticky=E)
self.indexEntry = Entry(self)
self.indexEntry.grid(row=1, column=2)
self.runBttn = Button(self, text="Run Function", command=self.psiFunction)
self.runBttn.grid(row=2, column=0, sticky=W)
self.answerLabel = Label(self, text="Output List:")
self.answerLabel.grid(row=2, column=1, sticky=W)
self.clearBttn = Button(self, text="Clear Output", command=)
self.clearBttn.grid(row=3, column=0, sticky=W)
def clear():
config.self.entryLabel(text="")
tk.Button(text="write", command=write).grid()
tk.Button(text="clear", command=clear).grid()
self.clearBttn = Button(self, text="Clear Output", command=clear)
self.clearBttn.grid(row=3, column=0, sticky=W)
You kinda asked two different questions here. I'll address the first, since that is what you came in with. To change the label, just update its text using the config method:
import Tkinter as tk
root = tk.Tk()
label = tk.Label()
label.grid()
def write():
label.config(text="Blah"*6)
def clear():
label.config(text="")
tk.Button(text="write", command=write).grid()
tk.Button(text="clear", command=clear).grid()
root.mainloop()