TKinter - Unable to update Text widget - python

I am trying to update a Text widget, but it's not updated no matter what I try, there is no error as well
def update():# a button calls this
textBox.delete(1.0, tk.END)
textBox.insert(tk.END,"test")
textBox = tk.Text(frame1,height=2,width=10)
textBox.config(state='disabled') #disable editing
textBox.grid(row=0,column=1,pady=2)

Using #JacksonPro's suggestion
import tkinter as tk
def update():# a button calls this
textBox.config(state="normal") # Make the state normal
textBox.delete("0.0", "end")
textBox.insert("end", "test")
textBox.config(state="disabled") # Make the state disabled again
root = tk.Tk()
textBox = tk.Text(root, height=2, width=10)
textBox.config(state="disabled") #disable editing
textBox.grid(row=0, column=1, pady=2)
button = tk.Button(root, text="Click me", command=update)
button.grid(row=1, column=1)
root.mainloop()

Related

How do I make this button be disabled and say "Booked" when clicked in tkinter

So, im designing a restaurant management system using tkinter and im trying to make the table booking system right now ive got a button setup to book the table and i want it so when its clicked it becomes gray and says booked but idk how to do that
can anyone help
thanks
window = Tk()
window.title("Restaurant Manager V2")
window.geometry("600x300")
#setup tkinter label
label1 = Label(window, text="Click button to book table")
label1.grid(row=0, column=1, sticky=W)
#setup tkinter button
def button_click():
button1 = Button(window, text="Table 1", width=5, command=button_click)
button1.grid(row=2, column=0, sticky=W)
window.mainloop()
def button_click():
button1.config(state="disabled", text="Booked")
you can this with 2nd way
def button_click():
button1 = Button(window, text="Booked !", width=5,
state="disabled")
button1.grid(row=2, column=0, sticky=W)

tkinter get selected element from combobox without button

I'm trying to find a way to get the tkinter combobox selected element without using a button, or to get the value from command in button, however so far nothing is working for me.
here's an example code (that's not working):
def show_frame(frame, prev_frame):
selected_elem = combobox.get()
if selected_elem == "choose element":
label = Label(frame1, text="please choose an element!")
label.grid(row=4, column=0)
else:
prev_frame.grid_forget()
frame.grid(row=0, column=0, sticky='nsew')
return selected_elem
elem= ""
button = Button(frame1, text="enter", command=lambda: elem==show_frame(frame3, frame1))
button.grid(row=2, column=1, padx=10, pady=10)
Is there a way to get it also outside of this function? this was just an idea that I had but as I mentioned, it's not working...
You need the bind method to watch for a selected element:
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
root.title("Combobox")
selected_elem = tk.StringVar(value='value1')
combobox = ttk.Combobox(root, textvariable=selected_elem)
combobox['values'] = ('value1', 'value2', 'value3')
combobox.pack(fill=tk.X, padx=20, pady=20)
myLabel = tk.Label(root, text=selected_elem.get())
myLabel.pack()
# prevent typing a value
combobox['state'] = 'readonly'
# place the widget
combobox.pack(fill=tk.X, padx=5, pady=5)
# bind the selected value changes
def value_changed(event):
""" handle the value changed event """
myLabel.configure(text=selected_elem.get())
myLabel.pack()
combobox.bind('<<ComboboxSelected>>', value_changed)
root.mainloop()
more info here:
https://www.pythontutorial.net/tkinter/tkinter-combobox/

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 set default value of radio button using TKinter in a class?

I'm trying to set the default value of a radio button using TKinter and Python. It's my first time using it so I'm pretty new. My understanding is that the default value should be set to the second radio button in my example (value=1).
from tkinter import *
from tkinter import ttk
class RadioButtons:
def __init__(self, root):
self.root = root
self.jobNum = IntVar(value=1)
self.create()
def create(self):
content = ttk.Frame(self.root)
radioButtons = ttk.LabelFrame(content, borderwidth=5, relief="ridge", width=400, height=400, text="Radio Buttons")
radioButtonsLbl=ttk.Label(radioButtons, text="Buttons")
# radio buttons
jobType1 = ttk.Radiobutton(radioButtons, text="Button 0", variable= self.jobNum, value=0)
jobType2 = ttk.Radiobutton(radioButtons, text="Button 1", variable= self.jobNum, value=1)
jobType3 = ttk.Radiobutton(radioButtons, text="Button 2", variable= self.jobNum, value=2)
content.grid(column=0, row=0)
# add to grid
radioButtons.grid(column=0, row=0, columnspan=3, rowspan=3)
radioButtonsLbl.grid(column=0, row=5, padx=20, pady=5, sticky=W)
jobType1.grid(column=1, row=5, padx=20, pady=0, sticky=W)
jobType2.grid(column=1, row=6, padx=20, pady=0, sticky=W)
jobType3.grid(column=1, row=7, padx=20, pady=0, sticky=W)
root = Tk()
RadioButtons(root)
root.mainloop()
However no radio button is selected when running the program. (screenshot of program)
The debugger confirms that the value of self.jobNum is set correctly.(screenshot of debugger)
How do I set the default value? I've tried a number of things including self.jobNum.set() before and after creating and adding the radio buttons but to no avail.
What am I missing here? Is this some kind of scope issue?
I suspect this has something to do with python's garbage collector. I can make the problem go away by saving a reference to RadioButtons(root):
root = Tk()
rb = RadioButtons(root)
root.mainloop()

How do I make my button know what`s in the entry box and then print that value in a new window?

I want after you write something in the entry box and then you press the button a new window to pop up and
the number that was written in the entry box to be printed in the new window as " Your height is: "value" but after many tries I still don`t understand how to do it.
my code:
import tkinter as tk
root = tk.Tk()
root.geometry("250x130")
root.resizable(False, False)
lbl = tk.Label(root, text="Input your height", font="Segoe, 11").place(x=8, y=52)
entry = tk.Entry(root,width=15).place(x=130, y=55)
btn1 = tk.Button(root, text="Enter", width=12, height=1).place(x=130, y=85) #command=entrytxt1
root.mainloop()
This is what I got:
import tkinter as tk
root = tk.Tk()
root.resizable(False, False)
def callback():
# Create a new window
new_window = tk.Toplevel()
new_window.resizable(False, False)
# `entry.get()` gets the user input
new_window_lbl = tk.Label(new_window, text="You chose: "+entry.get())
new_window_lbl.pack()
# `new_window.destroy` destroys the new window
new_window_btn = tk.Button(new_window, text="Close", command=new_window.destroy)
new_window_btn.pack()
lbl = tk.Label(root, text="Input your height", font="Segoe, 11")
lbl.grid(row=1, column=1)
entry = tk.Entry(root, width=15)
entry.grid(row=1, column=2)
btn1 = tk.Button(root, text="Enter", command=callback)
btn1.grid(row=1, column=3)
root.mainloop()
Basically when the button is clicked it calls the function named callback. It creates a new window, gets the user's input (entry.get()) and puts it in a label.

Categories