Tkinter change text color while writing - python

So, i want to change text color while writing, i tried this code which did nothing apparently:
def text_color():
txt.tag_configure('color',foreground='red')
txt.tag_add('color','1.0',END)
for example, when i am writing a text in Text() widget field by default it is black:
i want to write in text field with red color:

If you want to set the text color to red in the whole text widget, don't use tags, use the foreground option:
import tkinter as tk
root = tk.Tk()
txt = tk.Text(root, foreground='red')
txt.pack()
root.mainloop()

I have a running project, tag_config work for me. The other content is the same as yours.

Related

How can change the font for the title of the main frame in Python

# root window
root = tk.Tk()
root.geometry("550x400")
root.resizable(False, False)
root.title("Split files by bookmark")
The heading "Split files by bookmark" is small and faint and I want to make it display large, bold and colored. Prefixing and suffixing the string with double asterisks or special codes does not work for me. I do not know how to include markdown text.
Tkinter doesn't give you the ability to change the font that is used by the OS for the window, or to change the colors. You'll have to find some sort of platform-specific tool to do that.

How do I insert image

How do I insert image in my python-tkinter project?
I wanted to insert image in my tkinter Button, I tried it with this code:
photo = PhotoImage(r"Link to folder containing image\image.png")
photoimage = photo.subsample(3,3) # because I want image+text in button
st = Button(root, text='Sample', bg='grey', width=15, height=7, command=callback , image = photoimage, compound= LEFT, font=('Comic Sans Ms',20,'italic'))
st.grid(row=2, column=0, pady=20, side = TOP)
but this code dosen't work, I am using SubLime Text 3 IDE to build my projects
This is the code you need:
from PIL import ImageTk, Image
photo = ImageTk.PhotoImage(Image.open("1.Files/UI/edit.png")
Hope that helps!
The problem might be in the file path. Try escaping it. that means that you have to put two backslashes in a row instead of one when you're writing the path because it thinks that you're trying to escape a special character. you also need to make sure that you pass the path to the file parameter by typing file= before the path.
photo = PhotoImage(file="Link to folder containing image\\image.png")
If the button isn't showing up altogether, then you might have forgotten to pack the Button. Try adding .pack(side=TOP) at the end of the line where you define the button (st = Button(...).pack(side=TOP))
This worked for me.

How to add an area for a block of writing in python tkinter gui

I have attached a screenshot of this person's gui and was wondering if anyone knows how he got the white box on the right.
In the video it does not show but is it a label or something? I am quite new and have never seen this before. I need to add my own data into it using variables but I don't know how to format the entire box.
It is a Text widget:
from tkinter import *
root = Tk()
t = Text(root)
t.pack()
root.mainloop()
You can also change the size by doing t = Text(root, width=50, height=25). Height is actually the number of lines and width the number of characters on one line, not pixels. For more formatting options, go here.

How to change label text in Pygubu / Python

Im trying to change label text property of Label in Pygubu GUI builder for Tkinter with code:
fps_lable = self.builder.get_object('FPS_Label')
txt = fps_lable.cget('text')
print(txt)
fps_lable.config(text='hello')
But nothing happens. I get the text from label and it prints out (txt var) but when trying to change the text to "hello"... no joy.
Any ideas why ?
Try
fps_lable = self.builder.get_object('FPS_Label')
fps_lable.set('hello') # sets label to 'hello'
NOTE: FPS_Label must be the name of the 'textvariable' for the label in the gui.

Tkinter : How to change position of text on image

I have developed some application on my laptop with python and tkinter. Then, I was stuck at some point. Question is : how can I change text position on image.
import tkinter as tk
from PIL import Image, ImageTk
path_to_pic = "....."
root = tk.Tk()
pic = Image.open(path_to_pic)
tkpic = ImageTk.PhotoImage(pic)
tk.Label(root, image = tkpic, text = ".....", compound = tk.CENTER).pack()
root.mainloop()
This shows that my text appears on the picture, only on the center. I would like to move my text little by little and find best position. Do you know any solution or similar way to achieve this ?
You can move text horizontally and vertically by adding spaces and '\n's respectively, to any side(s) of the text you wish:
text = "caption \n\n\n\n\n\n\n"
This will put "caption" at the top left of the text.

Categories