how to set value in Text widget in tkinter? - python

My problem: I can't display the value to Text widget when I select a row.
how can I display a value inside the Text widget from the selected row?
I tried using textvariable attribute in Text widget but its not working.

Use insert method of the Text widget. Minimal example:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root)
text.pack()
def add_text():
# text.delete(1.0, tk.END) # Uncomment if you need to replace text instead of adding
text.insert(tk.END, f"Some text\n")
tk.Button(root, text="Add text", command=add_text).pack()
root.mainloop()

Related

Extracting text from Tkinter in python is printing none

I want that to print value that one enters in input box gets printed, and later I'll use it with SQL, but when I run this, it prints with None in terminal.
from tkinter import *
from tkinter import ttk
window=Tk()
def submit():
label.config(text=""+text.get(1.0, "end-1c"))
pri = label.config(text=""+text.get(1.0, "end-1c"))
print(str(pri))
# Add a text widget
text=Text(window, width=80, height=15)
text.insert(END, "")
text.pack()
# Create a button to get the text input
b=ttk.Button(window, text="Print", command=submit)
b.pack()
# Create a Label widget
label=Label(window, text="", font=('Calibri 15'))
label.pack()
window.mainloop()

Python Tkinter how can I place background text in a textbox?

I am trying to place a greyed out background text inside a textbox, that disappears when someone begins to type. I have tried overlaying a label onto the textbox, but I could not get it to work. Here is some code I am running for the text box
root = tk.Tk()
S = tk.Scrollbar(root)
T = tk.Text(root, height=70, width=50)
S.pack(side=tk.RIGHT, fill=tk.Y)
T.pack(side=tk.LEFT, fill=tk.Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set)
root.protocol("WM_DELETE_WINDOW", stop)
tk.mainloop()
How can I put in the background text?
Using a callback function you can remove the default text and change the foreground colour
import tkinter as tk
root = tk.Tk()
e = tk.Entry(root, fg='grey')
e.insert(0, "some text")
def some_callback(event): # must include event
e.delete(0, "end")
e['foreground'] = 'black'
# e.unbind("<Button-1>")
e.unbind("<FocusIn>")
return None
# e.bind("<Button-1>", some_callback)
e.bind("<FocusIn>", some_callback)
e.pack()
root.mainloop()
You are probably talking about placeholders , tkinter does not have placeholder attribute for Text widget, but you do something similar with Entry widget . You have to do it manually, by binding an event with the Text widget.
text = tk.StringVar()
text.set('Placeholder text')
T = tk.Entry(root, height=70, width=50, textvariable = text)
def erasePlaceholder(event):
if text.get() == 'Placeholder text':
text.set('')
T.bind('<Button-1>', erasePlaceholder)
Try to ask if you face any issues.

Tkinter: Show./Hide Specific Text in a Textbox

DESCRIPTION:
I have a textbox with text in it. See the image below.
QUESTION:
I want the highlighted text to hide when I click the "Hide" button. And then show the text, when I click the Show button (not there in the picture).
Similar to pack() and pack_forget(), but this time, for text and not widget.
You can add a tag to a region of text, and configure the tag with elide=True to hide the text, and set it to elide=False to show it.
Here's a little example:
import tkinter as tk
def hide():
text.tag_add("hidden", "sel.first", "sel.last")
def show_all():
text.tag_remove("hidden", "1.0", "end")
root = tk.Tk()
toolbar = tk.Frame(root)
hide_button = tk.Button(toolbar, text="Hide selected text", command=hide)
show_button = tk.Button(toolbar, text="Show all", command=show_all)
hide_button.pack(side="left")
show_button.pack(side="left")
text = tk.Text(root)
text.tag_configure("hidden", elide=True, background="red")
with open(__file__, "r") as f:
text.insert("end", f.read())
toolbar.pack(side="top", fill="x")
text.pack(side="top", fill="both", expand=True)
text.tag_add("sel", "3.0", "8.0")
root.mainloop()

Display text on a Tkinter label

I would like to know how to display text or a number to an empty label, after a button is clicked. Let's pretend, I want to display the number "0" to an empty label after a button is clicked.
My questions in simplified form are:
How to set a numeric value of 0 to a button.
Once the numeric integer value of (0) is set, how do you display the result to an empty label after the button widget is clicked?
You need to bind a callback helper method which would be triggered when the button is clicked. Here's an MCVE:
import tkinter as tk
root = tk.Tk()
label = tk.Label(root)
label.pack()
button = tk.Button(
root,
text='Click Me!',
command=lambda: label.configure(text='beautiful spam')
)
button.pack()
root.mainloop()
...you'd do the same for 0 - the text doesn't matter, nor does it matter whether you're configuring an empty label, or a label which already has some text.

Insert widget (Label) into Listbox in Tkinter

Hi people is it possible to insert Label widget into Listbox in tkinter instead of some str value and attach to it a scrollbar?
Based on the documentation online, No
The listbox can only contain text items...
for further details on the ListBox: http://effbot.org/tkinterbook/listbox.htm
Don't ask me to explain it because I really don't understand it yet, but this should work:
import tkinter as tk # python3
root = tk.Tk()
myList = tk.Listbox(root)
myText = "A list item"
myList.insert(tk.END, myText)
myList.pack()
lbl = tk.Label(myList, text=myText, anchor="w", font=("Helvetica", "24"))
lbl.pack(side="top", fill="x", anchor="w")
root.mainloop()

Categories