I tried to change the label text by pressing the "+" button, but if I run the program it is already changed to "testok" instead of "test" at start. So my question is why?
from tkinter import *
root = Tk()
var = "test"
label = Label(root, text=var)
label.pack()
button_plus = Button(root, text="+", command=label.config(text=var + "ok"))
button_plus.pack()
button_minus = Button(root, text="-", command=root.destroy)
button_minus.pack()
root.mainloop()
The command of button_plus is assigned the result of label.config(text=var+"ok") which is None. You can use lambda to do what you want:
button_plus = Button(root, text="+", command=lambda: label.config(text=var + "ok"))
Related
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()
This question already has answers here:
Why isn't this label changing when I use the tkinter config option
(2 answers)
Closed 1 year ago.
I want this label to configure into the text entry after the user enters the text and hits go but the label isn't configuring.
I want the label that says "Hello!" to change into whatever is put in the main entry. I'm looking for an answer written in full code instead of one fixed line.
Here's my code:
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True)
exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
def answer():
answer_label.config(text=main_entry.get())
entry_frame = tk.Frame(root)
main_entry = tk.Entry(entry_frame, width=100)
main_entry.grid(row=0, column=0)
go_button = tk.Button(entry_frame, text= 'Go!', width=85, command= answer)
go_button.grid(row=1, column=0)
answer_label = tk.Label(text = "Hello!").pack()
entry_frame.place(relx=.5, rely=.5, anchor='center')
root.mainloop()
1.Split tk.Label and pack().
2.Pass the lable.
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True)
exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
def answer(answer_label):
answer_label.config(text=main_entry.get())
entry_frame = tk.Frame(root)
main_entry = tk.Entry(entry_frame, width=100)
main_entry.grid(row=0, column=0)
answer_label = tk.Label(text = "Hello!")
answer_label.pack()
go_button = tk.Button(entry_frame, text= 'Go!', width=85, command=lambda: answer(answer_label))
go_button.grid(row=1, column=0)
entry_frame.place(relx=.5, rely=.5, anchor='center')
root.mainloop()
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
So I've been having a weird bug with Python and Tkinter where a message box pops up too early. I don't know what's wrong: it should be working, right?
from tkinter import *
from tkinter import messagebox
with open("passwords.passwords", "a+") as f:
root = Tk()
root.geometry("1000x1000")
root.wm_title("Info Collect")
mylist = []
var1 = StringVar()
var1.set("Application:")
label1 = Label(root, textvariable=var1, height=2)
label1.grid(row=0, column=0)
var2 = StringVar()
var2.set("Password:")
label2 = Label(root, textvariable=var2, height=2)
label2.grid(row=0, column=3)
ID1 = StringVar()
ID2 = StringVar()
box1 = Entry(root, bd=4, textvariable=ID1)
box1.grid(row=0, column=1)
box2 = Entry(root, bd=5, textvariable=ID2)
box2.grid(row=0, column=4)
def get_info():
f.write("{0}: {1} ".format(box1.get(), box2.get()))
def output_info():
messagebox.showinfo(f.read())
buttonA = Button(root, text="Save info", command=get_info, width=8)
buttonA.grid(row=0, column=2)
buttonB = Button(root, text="Output info", command=output_info(), width=8)
buttonB.grid(row=0, column=5)
root.mainloop()
That is all the code, do I have to do anything to it?
Change this
buttonB = Button(root, text="Output info", command=output_info(), width=8)
to
buttonB = Button(root, text="Output info", command=output_info, width=8)
with parenthesis, the function is called and the returned value will be set to command, when you remove parenthesis you pass the function itself to command to be called when the button is clicked
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()
What I would like to occur is that when I press the other button, the first label is destroyed and only the corresponding label is on the GUI. Is there a way to incorporate If statements into this or should I approach it another way?
from tkinter import *
root = Tk()
root.geometry("250x50")
def func1():
label = Label(root, text = 'Hello', fg="White", bg="Orange" )
label.pack(fill=BOTH, expand=True)
def func2():
label = Label(root, text = 'Goodbye', fg="White", bg="Orange" )
label.pack(fill=BOTH, expand=True)
button1 = Button(root, text = "Button 1", command = func1, fg="White",
bg="Black", width=10, height=5)
button1.pack(side=LEFT)
button2 = Button(root, text = "Button 2", command = func2, fg="White",
bg="Black", width=10, height=5)
button2.pack(side=LEFT)
root.mainloop()
Here is the approach that #jasonsharper proposed: It is indeed easier to have a single Label, created at the start, then to use the two buttons to set its text, and other properties.
import tkinter as tk
if __name__ == '__main__':
root = tk.Tk()
root.geometry("250x50")
def set_label(txt):
label['text'] = txt
label['fg'] = "White"
label['bg'] = "Orange"
button1 = tk.Button(root, text = "Button 1", command = lambda x='hello': set_label(x), fg="White", bg="Black", width=10, height=5)
button1.pack(side=tk.LEFT)
button2 = tk.Button(root, text = "Button 2", command = lambda x='bye': set_label(x), fg="White", bg="Black", width=10, height=5)
button2.pack(side=tk.LEFT)
label = tk.Label(root, text='')
label.pack(fill=tk.BOTH, expand=True)
root.mainloop()
Note:
Please avoid import * --> to keep your namespace clean, use import tkinter as tk