How to edit entry widget value if user press a button? - python

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)

Related

how would i have one entry box display individual entries into different places?

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

How can I store data on Toplevel?

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.

Automatically updating a value through a OptionMenu tkinter object

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

Get checkbox to create/delete window when checked/unchecked

I want a checkbox that when check, creates a scrolled text widget and when unchecked, removes the widget.
Currently it creates the widget only once I've checked the box and then unchecked it, and then when checked again, it does nothing and when unchecked again, it creates a second widget below.
I've tried different ways of coding it but I can't figure out what I'm doing wrong.
# Creates Normal Checkbutton
chk_state = BooleanVar()
chk_state.set(False) # set check state
chk = Checkbutton(window, text='Normal Entries', var=chk_state)
chk.place(x=0, y=0)
#Checks Checkbutton State
def chk_checked(event):
txt = scrolledtext.ScrolledText(window, height=15, width=35)
if chk_state.get():
txt.insert(END, 'Paste Normal Entries Here...')
txt.pack(anchor='nw', padx=50, pady=50)
elif txt.winfo_exists():
txt.pack_forget()
else:
pass
#Event when checkbox checked
chk.bind('<Button-1>', chk_checked)
You can try as this
import tkinter as tk
from tkinter.scrolledtext import ScrolledText
def chk_checked():
global txt
if chk_state.get():
txt = ScrolledText(window, height=15, width=35)
txt.insert(tk.END, 'Paste Normal Entries Here...')
txt.pack(anchor='nw', padx=50, pady=50)
else:
txt.pack_forget()
window = tk.Tk()
chk_state = tk.BooleanVar()
chk_state.set(False) # set check state
chk = tk.Checkbutton(window, text='Normal Entries', var=chk_state, command=chk_checked)
chk.place(x=0, y=0)
txt = None
window.mainloop()
This isn't the best way for do that, maybe you can create a class, i think that would be better.
The problem with your code is that each time that you click the CheckButton the function chk_checked(event) creates a new ScrolledText and after works on it instead working on the ScrolledText that was created previously. You have to declare a global variable (instead of a local variable) in wich you store the ScrolledText that you want to use and work only with it

When an entry box is present, a button does not show up

I have been doing some work in python and have came across Tkinter which is very useful for my project. I was in the process of making a screen where there is a button and a text entry box however when the text entry box is present, no button shows up. However when I remove the entry box, I can see the button.
Here is the part of the script I've been working on:
Hi, I have been doing some work in python and have came across Tkinter which is very useful for my project. I was in the process of making a screen where there is a button and a text entry box however when the text entry box is present, no button shows up. However when I remove the entry box, I can see the button.
from tkinter import *
def submit1():
print("working");
def password1():
passwordbox= Tk()
passwordbox.title("Password Verification")
passwordbox.configure(background="white")
passwordbox.geometry("1000x1000")
box1 = Entry(passwordbox, width=200, bg="gray")
box1.grid(row=2000, column=10, sticky=W)
submit = Button(passwordbox, text="Submit", width=20, height=5,
bg="black", fg="white", command=submit1)
submit.grid(row=1000, column=15, sticky=W);
password1()
The text box should show to entry box and the button however it only shows the button
If the entry box code was # out, the button will work
Anyone got any ideas?
You need to add a line passwordbox.mainloop() at the end of password1 definition. Also you need to specify row and column of the grid properly.
Set Entry box to row 0 and column 0
Set Submit button to row 1 and column 0
from tkinter import *
def submit1():
print("working");
def password1():
passwordbox= Tk()
passwordbox.title("Password Verification")
passwordbox.configure(background="white")
passwordbox.geometry("1000x1000")
box1 = Entry(passwordbox, width=200, bg="gray")
box1.grid(row=0, column=0, sticky=W)
submit = Button(passwordbox, text="Submit", width=20, height=5,
bg="black", fg="white", command=submit1)
submit.grid(row=1, column=0, sticky=W);
passwordbox.mainloop()
password1()

Categories

Resources