On the toplevel window, if the toplevel is closed after get some input from the user using the entry widget, and then the same toplevel is opened by pressing the same button, is there a way to see the entry we received from the user in the entry widget?
For example user enter his name on toplevel window, and close the toplevel.Then the user open same toplevel,i want it to see his name in the entry widget.
Try this:
import tkinter as tk
last_user_input_entry = ""
last_user_input_button = 0
def on_closing():
global entry, top, last_user_input_entry, last_user_input_button, button_var
text = entry.get()
last_user_input_entry = text
last_user_input_button = button_var.get()
print("The text entered =", last_user_input_entry)
print("Checkbutton state =", last_user_input_button)
top.destroy()
def start_top():
global entry, top, button_var
top = tk.Toplevel(root)
top.protocol("WM_DELETE_WINDOW", on_closing)
entry = tk.Entry(top)
entry.pack()
entry.insert("end", last_user_input_entry)
button_var = tk.IntVar()
button_var.set(last_user_input_button)
button = tk.Checkbutton(top, variable=button_var)
button.pack()
root = tk.Tk()
button = tk.Button(root, text="Open toplevel", command=start_top)
button.pack()
root.mainloop()
Basically we intercept the window closing and handle that our self. We also have a variable that stored the last user input and we put it in the tkinter.Entry after we create it.
Related
I am essentially trying to have my entry box display the entered text on the screen as a label, expect after each time the button is pressed (the button that displays the text), the newly entered data is displayed somewhere else on the screen. Essentially multiple labels from one entry box and button.
def writelabel():
labelentrval = Label(master, font = ("Helvetica", 15), text="" + str(entryvalue.get()))
labelentrval.place(x=645,y=621)
entryvalue.delete(first=0,last=22)
()
entryvalue = Entry(master)
entryvalue.place(x=700, y=515)
buttonpressy = Button(master,text="Enter", command=writelabel)
buttonpressy.place(x=845,y=510)
Code is above, and so far when I enter something into my entry box and press the button it displays it on the screen as a label. But how would I keep displaying my entries over and over in different places?
Thank you
You want to create a new label for every button press:
from tkinter import *
root = Tk()
def clicked():
text = e.get()
newLabel = Label(root, text=text)
newLabel.pack()
e = Entry(root)
e.pack()
b = Button(root, text='click', command=clicked)
b.pack()
root.mainloop()
from tkinter import *
root = Tk()
label = Label(root, text="Name:")
label.pack()
e = Entry(root)
e.pack()
root.mainloop()
How do I get a user input in entry widget without button in tkinter?
window = tk.Tk()
username = tk.Entry(window)
username.pack()
def it_has_been_written(*args):
#what you want to execute when user starts typing something
pass
username.trace_add("write", it_has_been_written)
window.mainloop()
To see more about this https://tkdocs.com/tutorial/widgets.html
I would like to write a tkinter app that will automatically update a value based on the current state of the OptionMenu object. Here's what I have so far
from tkinter import *
root = Tk()
def show():
myLabel=Label(root,text=clicked.get()).pack()
clicked=StringVar()
clicked.set("1")
drop = OptionMenu(root,clicked,"1","2","3")
drop.pack()
myButton = Button(root,text="show selection",command=show)
root.mainloop()
In this version, the text can only be updated by clicking a button. How can I make the text update automatically, without this "middle man"?
You can simply assign clicked to the textvariable of the Label, then whenever an option is selected, the label will be updated:
import tkinter as tk
root = tk.Tk()
clicked = tk.StringVar(value="1")
drop = tk.OptionMenu(root, clicked, "1", "2", "3")
drop.pack()
tk.Label(root, textvariable=clicked).pack()
root.mainloop()
After changing some things, i got it working.
It is better to use the config() function to change item's attributes, and another important thing is to not pack() the objects (the Label, in this case) in the same line that the variable declaration.
Like so, you'll be able to change the text. Here is your code updated!
from tkinter import *
def show():
myLabel.config(text = clicked.get())
root = Tk()
clicked=StringVar( value="1")
myLabel=Label(root, text="click the button at the bottom to see this label text changed")
myLabel.pack()
drop = OptionMenu(root, clicked, "1","2","3")
drop.pack()
myButton = Button(root, text="show selection", command=show)
myButton.pack()
root.mainloop()
I'm making my own calculator using python and tkinter however I can't seem to find a way to change the value in the entry field. Let's say once the user taps the equal button, the value in the entry field changes to the answer of the user's equation. So how do you do it?
import Tkinter as tk
top = tk.Tk()
user_ent = tk.Entry(top, width='7', font='Verdana 50', justify='right')
user_ent.grid(columnspan='4')
button = tk.Button(top, text='clear', command=None) #button that clears the entry field
button.grid(column='0', row='1', columnspan='1')
top.mainloop()
How to you make the button clear the entry field once it's clicked
def solve():
txt = user_ent.get(0, tk.END)
user_ent.delete(0, tk.END)
user_ent.insert(tk.INSERT, eval(txt))
button = tk.Button(top, text='clear', command=solve)
I am just trying to make a non-graphic game in Tkinter but am having trouble with the entry widget. How can I wait for "<Return>" to be pressed before printing something?
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
f = Frame(master, width=500, height=300)
f.pack(fill=X, expand=True)
mt = StringVar()
menubar = Menu(master)
menubar.add_command(label="New Game", command=self.new_game)
menubar.add_command(label="Continue Game", command=self.continue_game)
master.config(menu=menubar)
maintext = Label(master, fg="blue", textvariable=mt)
maintext.pack(side=BOTTOM)
mt.set("")
self.e1 = Entry(master)
self.e1.pack()
self.e1.bind("<Return>")
self.e1.lower()
global mt
def new_game(self):
mt.set("What do you want your username to be?")
self.e1.lift()
#wait for <Return> to be pressed
mt.set("Welcome " + self.e1.get())
def continue_game(self):
mt.set("Type your username in.")
root = Tk()
app = App(root)
root.mainloop()
I want it to be when I click "New Game" on the top menubar, it shows the entry box and waits for me to type something in, and then click enter. THEN it prints out "Welcome"+(what the person types in). What actually happens is when I click New game, it immediately just prints "Welcome".
By print, I mean aet the Label at the bottom to something else, which is reffered to by "mt.set".
In GUI programs you don't wait* for something to happen, you respond to events. So, to call a function when the user presses return you would do something like:
self.e1.bind("<Return>", self._on_return)
The above will call the function _on_return when the user presses the return key.
In your specific code you don't really need a StringVar. For example, you could do this:
def __init__(self, master):
...
self.maintext = Label(master, fg="blue")
...
def _on_return(self, event):
self.maintext.configure(text="Welcome, %s" % self.e1.get())
* strictly speaking, you can wait for things, but that's not the right way to write GUI programs except under specific circumstances, such as waiting for a dialog to be dismissed.