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.
Related
I'm trying to make a text editor app with python Tkinter and tabs with the notebook function. I have 1 problem, when I add the input text box to the screen it works perfectly I tried writing with it and yes it worked, but when I switched to another tab the same text is there and I tried multiple solutions like functions that make another input and if statements, I also saw a StackOverflow post that showed how to use if statements (I don't have the link) but it still didn't work for me.
here is my code :
nb = ttk.Notebook(window)
tab1 = ttk.Frame(nb)
tab2 = ttk.Frame(nb)
nb.add(tab1, text ='Tab 1')
nb.add(tab2, text ='Tab 2')
nb.pack(expand = 1, fill ="both")
ttk.Label(tab1)
ttk.Label(tab2)
editor_text_box = Text(
window,
height=12,
width=40
)
editor_text_box.pack()
You're not putting the text widget in a frame, you're putting it in the parent of the notebook.
If you want a widget to appear in a tab, it must be a child of the frame for that tab.
editor_text_box = Text(tab1, ...)
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.
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 can I change the text of a Python 3.5 tkinter Notebook tab programatically AFTER adding it to the notebook?
So I have added a tab with:
notebookWidget.add (tabWidget, text = 'myOldText')
which works fine.
Now, I want to do something like:
tabWidget.text = 'myNewText'
or, (more tkinter'ish?):
tabWidget ['text'] = 'myNewText'
Found it:
notebookWidget.tab (tabWidget, text = 'myNewText')
Correct answer is (where tab_idx is the 0 based index of the tab within the notebokk):
notebook_widget.tab(tab_idx, text='Another text')
There is a basic method inherited from Widget which is called configure() and does the job you expect:
tabWidget.configure(text = 'myNewText')
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.