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.
Related
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()
I need to create a field in tkinter that stores an emoji but when someone presses a button, that emoji is overwritten. I can't get emojis working in tkinter and I'm not sure how to then overwrite it.
import tkinter as tk
self.option4 = tk.Button(self, width=10)
self.option4["text"] = "no"
self.option4["command"] = self.wrong
self.option4.pack(side="top")
corecalc = "🔲"
self.answercheck = tk.Text(self, height=1, width=5)
self.answercheck.pack()
self.answercheck.insert(tk.END, corecalc)
self.QUIT = tk.Button(self, text="Quit", fg="red", command=root.destroy)
self.QUIT.pack(side="bottom")
def correct(self):
corecalc = "✅"
def wrong(self):
corecalc = "❌"
Expected 🔲 outputs in field and changes to ❌ upon button press. Also is there a better method than text box that makes the field fixed rather than editable by end user.
Error: _tkinter.TclError: character U+1f532 is above the range (U+0000-U+FFFF) allowed by Tcl
You can use any tkinter widget to display the characters - emojis or otherwise, that you will need - a Text widget if you want to display a single character is a poor choice, though possible.
If you really want to use Text, you can change it to not editable by setting its state Key to "disabled" (as oposed to the default "normal"):
self.answercheck = tk.Text(self, height=1, width=5, state="disabled")
A text box will require you to remove the previous text before insertig the new, you can simply use a tkinter.Label widget if you want programtic only changes to the characters:
import tkinter as tk
w = tk.Tk()
display = tk.Label(w, text="□")
display.pack()
def correct():
display["text"] = "✅"
def wrong():
display["text"] = "❌"
button = tk.Button(w, text="no", command=wrong)
button.pack()
button = tk.Button(w, text="yes", command=correct)
button.pack()
tkinter.mainloop()
(In the build I have here - Python 3.7 on a Linux fedora, tkinter can't handle your 🔲 character directly, as its codepoint is above \uffff)
I'm making a simple word processor and I want to be able to change the font/style (or whatever it's called) of only the text that I highlight.
I can't say what I've tried because I don't even know where to start.
from tkinter import *
# window setup
tk = Tk()
# main textbox
textbox = Text(tk)
textbox.configure(width=85,height=37)
textbox.grid(column=0,row=0,rowspan=500)
# bold
def bold():
textbox.config(font=('Arial',10,'bold'))
bBut = Button(tk,text='B',command=bold)
bBut.configure(width=5,height=1)
bBut.grid(column=0,row=0)
tk.mainloop()
I can change the entire text to bold/italic/etc. but I want to be able to specify parts of it.
It use tags to assign color or font to text
import tkinter as tk
def set_bold():
try:
textbox.tag_add('bold', 'sel.first', 'sel.last')
except Exception as ex:
# text not selected
print(ex)
def set_red():
try:
textbox.tag_add('red', 'sel.first', 'sel.last')
except Exception as ex:
# text not selected
print(ex)
root = tk.Tk()
textbox = tk.Text(root)
textbox.pack()
textbox.tag_config('bold', font=('Arial', 10, 'bold'))
textbox.tag_config('red', foreground='red')
button1 = tk.Button(root, text='Bold', command=set_bold)
button1.pack()
button2 = tk.Button(root, text='Red', command=set_red)
button2.pack()
root.mainloop()
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()
So I am trying to when the person clicks on the button I created, to display the text they wrote on the input field but for some reason when I click the button it displays:
.!entry
And I don't know what I am doing wrong since I am new to python so I wanted to know how to fix this problem, here's my code and thank you since any help is appreciated!
from tkinter import *
screen = Tk()
def print_input():
text2 = Label(screen, text=input_field)
text2.grid(row=1, columnspan=3)
text = Label(screen, text="Write to print:")
text.grid(row=0, column=0)
input_field = Entry(screen)
input_field.grid(row=0, column=1)
submit_button = Button(screen, text="Print!", fg="yellow", bg="purple",
command=print_input)
submit_button.grid(row=0, column=2)
screen.mainloop()
Change:
def print_input():
text2 = Label(screen, text=input_field)
to:
def print_input():
text2 = Label(screen, text=input_field.get())
# ^^^^^^
You're telling the text of the label to be set to the Entry widget instead of the Entry widget's content. To get the content of the Entry widget, use the .get() method.
The funky string you're seeing in the label is the tkinter name for the Entry widget.