Python Tkinter doesn't show the label when I press the button - python

I have one Input and one button. I want the value of the input (Entry) to when I press the button. When I type print(mtext) it works well, but when I put it in a Label it doesn't work.
Here is the code:
from tkinter import *
root = Tk()
root.title("Mohamed Atef")
root.geometry("900x600")
var = StringVar()
var.set("Please write something")
label = Label(root, textvariable=var, padx=10, pady=10)
#input
text = StringVar()
entry = Entry(root, textvariable=text)
def mohamed():
mtext = text.get()
mohamed = Label(root, textvariable=mtext)
mohamed.pack()
#button
buttonText = StringVar()
buttonText.set("Click me !")
button = Button(root, textvariable=buttonText, command=mohamed)
label.pack()
entry.pack()
button.pack()
root.mainloop()

Same as Flilp, your finished product would look like this
from tkinter import *
root = Tk()
root.title("Mohamed Atef")
root.geometry("900x600")
var = StringVar()
var.set("Please write something")
label = Label(root, textvariable=var, padx=10, pady=10)
#input
text = StringVar()
entry = Entry(root, textvariable=text)
def mohamed() :
mtext = text.get()
mohamed = Label(root, text=mtext)
mohamed.pack()
#button
buttonText = StringVar()
buttonText.set("Click me !")
button = Button(root, textvariable=buttonText, command=mohamed)
label.pack()
entry.pack()
button.pack()
root.mainloop()

If you just want text that's in your Entry to appear under your labels you could do:
def mohamed():
mohamed = Label(root, textvariable=text)
mohamed.pack()
Your code didn't work because value passed as textvariable should be tkinter StringVar() not string.
If you don't want the text to be constantly updated when you change your Entry you should do:
def mohamed():
mtext = text.get()
mohamed = Label(root, text=mtext)
mohamed.pack()

Related

Tkinter - Change Label Text/Entry on Button Click

I have two labels and entry fields (A & B). When I enter the username/password for "A Username/A Password", I want to click the "Submit" button, then have the labels/entry fields change to "B Username/B Password" and be able to click the "Submit" button again, using Tkinter.
Python Code
import tkinter as tk
root = tk.Tk()
a_user_var = tk.StringVar()
a_pass_var = tk.StringVar()
b_user_var = tk.StringVar()
b_pass_var = tk.StringVar()
def submit():
a_user = a_user_var.get()
a_pass = a_pass_var.get()
a_user_var.set("")
a_pass_var.set("")
b_user = b_user_var.get()
b_pass = b_pass_var.get()
b_user_var.set("")
b_pass_var.set("")
a_user_label = tk.Label(root, text="A Username")
a_user_entry = tk.Entry(root, textvariable=a_user_var)
a_pass_label = tk.Label(root, text="A Password")
a_pass_entry = tk.Entry(root, textvariable=a_pass_var, show="•")
b_user_label = tk.Label(root, text="B Username")
b_user_entry = tk.Entry(root, textvariable=b_user_var)
b_pass_label = tk.Label(root, text="B Password")
b_pass_entry = tk.Entry(root, textvariable=b_pass_var, show="•")
sub_btn = tk.Button(root, text="Submit", command=submit)
a_user_label.grid(row=0, column=0)
a_user_entry.grid(row=0, column=1)
a_pass_label.grid(row=1, column=0)
a_pass_entry.grid(row=1, column=1)
b_user_label.grid(row=0, column=0)
b_user_entry.grid(row=0, column=1)
b_pass_label.grid(row=1, column=0)
b_pass_entry.grid(row=1, column=1)
sub_btn.grid(row=2, column=0)
root.mainloop()
Current Result
Desired Result (after clicking Submit button)
There is no need to create a unique label and entry widgets for A and B. Instead just use one set of widgets and change the label's text upon pressing the button to B. If you need to store the contents of the entry widget, you can grab the label text and parse it to see which character the specific set belongs to.
For example:
import tkinter as tk
root = tk.Tk()
user_var = tk.StringVar()
pass_var = tk.StringVar()
entries = {}
def submit():
user = user_var.get()
passw = pass_var.get()
label_text = user_label["text"]
char = label_text.split()[0]
entries[char] = (user, passw)
if char == "A":
user_label["text"] = "B" + label_text[1:]
pass_label["text"] = "B" + pass_label["text"][1:]
user_var.set('')
pass_var.set('')
print(entries)
user_label = tk.Label(root, text="A Username")
user_entry = tk.Entry(root, textvariable=user_var)
pass_label = tk.Label(root, text="A Password")
pass_entry = tk.Entry(root, textvariable=pass_var, show="•")
sub_btn = tk.Button(root, text="Submit", command=submit)
sub_btn.grid(row=2, column=0)
user_label.grid(row=0, column=0)
user_entry.grid(row=0, column=1)
pass_label.grid(row=1, column=0)
pass_entry.grid(row=1, column=1)
root.mainloop()

Copy a label on tkinter and change the text on button click?

I have some program of this kind of type:
from tkinter import *
def apply_text(lbl_control):
lbl_control['text'] = "This is some test!"
master = Tk()
lbl = Label(master)
btn = Button(master, text="apply", command=lambda: apply_text(lbl))
lbl.pack()
btn.pack()
mainloop()
My aim now is to copy the text of the label lbl itself without any ability to change it. I tried the following way to solve the problem:
from tkinter import *
def apply_text(lbl_control):
lbl_control.insert(0, "This is some test!")
master = Tk()
lbl = Entry(master, state="readonly")
btn = Button(master, text="apply", command=lambda: apply_text(lbl))
lbl.pack()
btn.pack()
mainloop()
because of state = "readonly" it is not possible to change the text insert of lbl anymore. For that reason nothing happens if I click on the button apply. How can I change it?
There is a simple way to do that simple first change the state of entry to normal, Then insert the text, and then change the state back to readonly.
from tkinter import *
def apply_text(lbl_control):
lbl_control['state'] = 'normal'
lbl_control.delete(0,'end')
lbl_control.insert(0, "This is some test!")
lbl_control['state'] = 'readonly'
master = Tk()
lbl = Entry(master, state="readonly")
btn = Button(master, text="apply", command=lambda: apply_text(lbl))
lbl.pack()
btn.pack()
mainloop()
There is another way to do this using textvariable.
Code:(Suggested)
from tkinter import *
def apply_text(lbl_control):
eText.set("This is some test.")
master = Tk()
eText = StringVar()
lbl = Entry(master, state="readonly",textvariable=eText)
btn = Button(master, text="apply", command=lambda: apply_text(lbl))
lbl.pack()
btn.pack()
mainloop()

how to update getting results from an entry box automatically?

I want to update getting results from an entry box in a way that when an integer enters, the equivalent rows of entry boxes appear below that. I have written the below code to make it work using a button. However, I want to make it happen automatically without a button as I entered the number, the rows update. I checked one way of doing that is using the after(). I placed after after() in the function and out of the function but it is not working.
from tkinter import *
root = Tk()
root.geometry("400x400")
n_para = IntVar()
label1 = Label(root, text="Numeric parameters")
label1.grid(row=0, column=0)
entry1 = Entry(root, textvariable=n_para)
entry1.grid(row=0, column=1)
def update():
for i in range(1, n_para.get()+1):
entryX = Entry(root)
entryX.grid(row=i+1, column=0)
entryY = Entry(root)
entryY.grid(row=i+1, column=1)
entryZ = Entry(root)
entryZ.grid(row=i+1, column=2)
root.after(100, update)
root.after(1, update)
button1 = Button(root, text="update", command=update)
button1.grid(row=1, column=0)
root.mainloop()
You should try using the <KeyRelease> event bind.
import tkinter as tk
def on_focus_out(event):
label.configure(text=inputtxt.get())
root = tk.Tk()
label = tk.Label(root)
label.pack()
inputtxt = tk.Entry()
inputtxt.pack()
root.bind("<KeyRelease>", on_focus_out)
root.mainloop()
This types the text entered in real-time.
Edited Code with OP's requirement:
from tkinter import *
root = Tk()
root.geometry("400x400")
n_para = IntVar()
label1 = Label(root, text="Numeric parameters")
label1.grid(row=0, column=0)
entry1 = Entry(root, textvariable=n_para)
entry1.grid(row=0, column=1)
def upd(event):
x = entry1.get()
if not x.isnumeric():
x = 0
for i in range(1, int(x)+1):
entryX = Entry(root)
entryX.grid(row=i+1, column=0)
entryY = Entry(root)
entryY.grid(row=i+1, column=1)
entryZ = Entry(root)
entryZ.grid(row=i+1, column=2)
# root.after(100, update)
root.bind("<KeyRelease>", upd)
# button1 = Button(root, text="update", command=update)
# button1.grid(row=1, column=0)
root.mainloop()

How to return value to Entry Widget

I don't know how to return or print result to entry widget. I've tried to add code on the res function, but it still doesn't return the result that I want.
import tkinter as tk
from tkinter import *
win = tk.Tk()
win.title("Test")
win.configure(background='black')
enternumber = Label(win, text="Enter Number")
enternumber.grid(column=0, row=0)
var1 = IntVar()
txtenternumber = Entry(win, width=20, textvariable=var1)
txtenternumber.grid(column=1, row=0)
enternumber = Label(win, text="Enter Number")
enternumber.grid(column=0, row=1)
var2 = IntVar()
txtenternumber = Entry(win, width=20, textvariable=var2)
txtenternumber.grid(column=1, row=1)
def res():
result = var1.get()*var2.get()
#here the code i added
resultentry.bind('<Return>', res)
#Here the button should returning result
resultbutton = Button(win, text="Ok", command=res)
resultbutton.grid(column=2, row=2)
resultlabel = Label(win,text="Result")
resultlabel.grid(column=0, row=2)
#I want result printed here
resultentry = Entry(win, width=30)
resultentry.grid(column=1,row=2)
import tkinter as tk
from tkinter import *
win = tk.Tk()
win.title("Test")
win.configure(background='black')
enternumber = Label(win, text="Enter Number")
enternumber.grid(column=0, row=0)
var1 = IntVar()
txtenternumber = Entry(win, width=20, textvariable=var1)
txtenternumber.grid(column=1, row=0)
enternumber = Label(win, text="Enter Number")
enternumber.grid(column=0, row=1)
var2 = IntVar()
txtenternumber = Entry(win, width=20, textvariable=var2)
txtenternumber.grid(column=1, row=1)
def res():
result = var1.get()*var2.get()
resultentry.insert(0,result) # INSERTS RESULT INTO resultentry
resultentry.bind('<Return>', res)
resultbutton = Button(win, text="Ok", command=res)
resultbutton.grid(column=2, row=2)
resultlabel = Label(win,text="Result")
resultlabel.grid(column=0, row=2)
resultentry = Entry(win, width=30)
resultentry.grid(column=1,row=2)
win.mainloop()
Sorry, forgot to add this. Yes, you just need to use resultentry.insert(0, result)and you should be set. I also like using this as a good resource:
https://effbot.org/tkinterbook/entry.htm

Python tkinter quiz

I am making a quiz in python using the tkinter module and I am stuck on how to create a button that checks to see if the answer is correct or not. But I would put it in a procedure however the question is already in one.
import tkinter as tk
window = tk.Tk()
window.title("6 Questions")
window.geometry("500x150")
score = 0
def inst():
t = tk.Label(window, text="All you need to do is just answer each question with either a '1, 2, 3' or the actual word.")
t.pack()
def start():
root = tk.Tk()
root.title("question 1")
q = tk.Label(root, text="what type of input holds whole numbers")
q.pack()
a = tk.Label(root, text="1.) int")
a.pack()
b = tk.Label(root, text="2.) string")
b.pack()
c = tk.Label(root, text="3.) float")
c.pack()
ans = tk.Entry(root, width=40)
ans.pack()
#here is the button I want to verify the answer
sub = tk.Button(root, text="Submit")
sub.pack()
greet = tk.Label(window, text="Welcome to the 6 Question Quiz.")
greet.pack()
start = tk.Button(window, command=start, text="Start")
start.pack()
instr = tk.Button(window, text="Instructions", command=inst)
instr.pack()
end = tk.Button(window, text="Exit", command=exit)
end.pack()
Create a function that opens when the submit button is clicked and create RadioButtons rather than Labels.
Like this:
def gettingDecision():
if var.get() is 'True':
messagebox.showinfo('Congrats', message='You Are Correct.Score is {}'.format(score))
else:
messagebox.showinfo('Lose', message='You Are Wrong.')
Question1 = ttk.Label(frame1, text='Q.1.Where does a computer add and compare data ?')
Question1.grid(row=1, column=0, sticky=W)
var = StringVar()
Q1A = ttk.Radiobutton(frame1, text='[A] Hard disk', variable=var, value='False1')
Q1A.grid(row=2, column=0, sticky=W)
Q1B = ttk.Radiobutton(frame1, text='[B] Floppy disk', variable=var, value='False2')
Q1B.grid(row=3, column=0, sticky=W)
Q1C = ttk.Radiobutton(frame1, text='[C] CPU chip', variable=var, value='True')
Q1C.grid(row=4, column=0, sticky=W)
Q1D = ttk.Radiobutton(frame1, text='[D] Memory chip', variable=var, value='False3')
Q1D.grid(row=5, column=0, sticky=W)
submit = ttk.Button(frame1, text='Submit', command=gettingDecision)
submit.grid()
Please note that, ideal way to go would be using classes.
You can define a function, inside of a function.
import tkinter as tk
window = tk.Tk()
window.title("6 Questions")
window.geometry("500x150")
score = 0
def inst():
t = tk.Label(window, text="All you need to do is just answer each question with either a '1, 2, 3' or the actual word.")
t.pack()
def start():
def submit():
print (ans.get())
#or do whatever you like with this
root = tk.Tk()
root.title("question 1")
q = tk.Label(root, text="what type of input holds whole numbers")
q.pack()
a = tk.Label(root, text="1.) int")
a.pack()
b = tk.Label(root, text="2.) string")
b.pack()
c = tk.Label(root, text="3.) float")
c.pack()
ans = tk.Entry(root, width=40)
ans.pack()
#here is the button I want to verify the answer
sub = tk.Button(root, text="Submit", command=submit)
sub.pack()
greet = tk.Label(window, text="Welcome to the 6 Question Quiz.")
greet.pack()
startButton = tk.Button(window, command=start, text="Start")
startButton.pack()
instr = tk.Button(window, text="Instructions", command=inst)
instr.pack()
end = tk.Button(window, text="Exit", command=window.destroy)
end.pack()
window.mainloop()

Categories