I can't seem to change the size of my font in tkinter! No matter which size I choose, the button text displays the same. If I deleted the whole stlye line, it's displayed smaller.
Similarly, the font always looks the same, no matter what I choose.
I want to finetune the size and the font, can you please help me=?
import tkinter
import tkinter.ttk as ttk
from tkinter import font
root = tkinter.Tk()
frame = ttk.Frame(root)
frame.grid(column=0, row=0)
style = ttk.Style(root)
ttk.Button(frame, text="Open file", command=None).grid(column=0, row=1)
ttk.Style().configure("TButton", font=font.Font(family='wasy10', size=80)) #I can choose any value here instead of "80" and any font like "Helvetica" - nothing will change
root.mainloop()
You do not need to import font. ttk style has its own font argument.
Just put the style in the first option and the font size in the 2nd option.
I would also use the variable name to edit the style. Instead of calling:
ttk.Style().configure()
Do this:
style.configure()
Take a look at the below.
import tkinter
import tkinter.ttk as ttk
root = tkinter.Tk()
frame = ttk.Frame(root)
frame.grid(column=0, row=0)
style = ttk.Style(root)
style.configure("TButton", font=('wasy10', 80))
ttk.Button(frame, text="Open file", command=None, style="TButton").grid(column=0, row=1)
root.mainloop()
On the advice of Bryan Oakley in the comments here is a 2nd option that is close to what you are trying to do with fort.
This option saves a referent to the font object and then uses it to update the style.
import tkinter
import tkinter.ttk as ttk
from tkinter import font
root = tkinter.Tk()
frame = ttk.Frame(root)
frame.grid(column=0, row=0)
style = ttk.Style(root)
font = font.Font(family="wasy10", size=80)
style.configure("TButton", font=font)
ttk.Button(frame, text="Open file", command=None, style="TButton").grid(column=0, row=1)
root.mainloop()
Related
Hello I am very new with the python module Tkinter, I have written the code for a simple text editor. But I am not able to figure out how to change the background colour.
Help would be welcome thx.
Code:
from tkinter import *
import tkinter as tk
# from tkinter import filedialog
# from tkinter import font
root = Tk()
root.title('Flax')
root.iconbitmap('E:\editor.ico')
root.geometry('1200x660')
# Main Frame
my_frame = Frame(root)
my_frame.pack(pady=5)
# ScrollBar
scroll = Scrollbar(my_frame)
scroll.pack(side=RIGHT, fill=Y)
# Text Box
text = Text(my_frame, width=98, height=25, font=("Helvetica", 13), selectbackground="grey", selectforeground="white", undo=True)
text.pack()
scroll.config(command=text.yview)
root.mainloop()
The parameter you need to use is bg = "blue" for example source.
Implemented into your code:
from tkinter import *
import tkinter as tk
# from tkinter import filedialog
# from tkinter import font
root = Tk()
root.title('Flax')
root.iconbitmap('E:\editor.ico')
root.geometry('1200x660')
# Main Frame
my_frame = Frame(root)
my_frame.pack(pady=5)
# ScrollBar
scroll = Scrollbar(my_frame)
scroll.pack(side=RIGHT, fill=Y)
# Text Box
text = Text(my_frame, width=98, height=25, font=("Helvetica", 13), bg="grey", fg="white", undo=True)
text.pack()
scroll.config(command=text.yview)
root.mainloop()
I wonder if it's possible to insert an image or icon in the title of a Labelframe in tkinter ?
Maybe something like this lf = Labelframe(root, image='my_img'), the same as we can do with a button.
It would be very useful.
You can insert any widget as labelwidget option of LabelFrame.
If you want an image instead of text displayed in LabelFrame:
import tkinter as tk
from PIL import Image, ImageTk
w = tk.Tk()
w.geometry("400x500")
img_open = Image.open("example_1.png")
img = ImageTk.PhotoImage(img_open)
label_title = tk.Label(w, image=img) #important Do Not grid/pack/place it!
label_frame = tk.LabelFrame(w, labelwidget=label_title)
label_frame.grid(row=0, column=0)
#fill LabelFrame with something to make it visible
bt = tk.Button(label_frame, text="Press")
bt.grid(padx=20, pady=20)
w.mainloop()
Output:
Or like I said before you can use any widget.
An example label with image and a label with text.
import tkinter as tk
from PIL import Image, ImageTk
w = tk.Tk()
w.geometry("400x500")
img_open = Image.open("example_1.png")
img = ImageTk.PhotoImage(img_open)
frame_labelwidget = tk.Frame(w) #important Do Not grid/pack/place it!
label_image = tk.Label(frame_labelwidget, image=img)
label_image.grid(row=0, column=0)
label_text = tk.Label(frame_labelwidget, text="Teamwork")
label_text.grid(row=0, column=1)
label_frame = tk.LabelFrame(w, labelwidget=frame_labelwidget)
label_frame.grid(row=0, column=0)
#fill LabelFrame with something to make it visible
bt = tk.Button(label_frame, text="Press")
bt.grid(padx=20, pady=20)
w.mainloop()
Output:
Of course that makes less sense because you can have text and an image combined in tk.Label by using compound option.
I added it only for demonstration purpose, you can add any widget you want. Instead of label_text you could add an Entry, Canvas...
I set the theme of my main window to an awtheme 'awdark'.All the widgets with ttk extension set its appearance according to the theme by itself all except for the scrolled text widget which appears white in colour(i.e, color of the field as well as the color and look of the scrollbar)probably because its not a part of ttk.My scrolledtext widget is contained in a ttk.Frame widget by the way.Is there any workaround this?
Is there a ttk equivalent of Scrolledtext widget Tkinter
No, there is not. The ttk widgets don't have a text widget.
The scrolledtext widget is just a text widget and scrollbars, there's not much more to it. You can create your own which uses ttk scrollbars with just a few lines of code.
Here's a solution that doesn't use classes. One that is class-based is just a couple extra lines of code.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
frame = ttk.Frame(root)
frame.pack(fill="both", expand=True)
text = tk.Text(frame, wrap="none")
vsb = ttk.Scrollbar(frame, command=text.yview, orient="vertical")
hsb = ttk.Scrollbar(frame, command=text.xview, orient="horizontal")
text.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
vsb.grid(row=0, column=1, sticky="ns")
hsb.grid(row=1, column=0, sticky="ew")
text.grid(row=0, column=0, sticky="nsew")
root.mainloop()
I am currently trying to learn ttk themed widgets. I wanted to change the background colour of my ttk button. I followed to ttk docs and written this:
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.title("GUI App One")
root.geometry("800x500")
root.maxsize(800,500)
root.minsize(800,500)
Style().configure("C.TButton", padding=6, background="blue", relief="raised")
Style().configure("B.TButton", font=("Arial",30))
Style().configure("Elem.TFrame", background="red")
backframe = Frame(root, width=800, height=500, style="Elem.TFrame")
backframe.place(x=0, y=0)
print()
btn1 = Button(backframe, text="Click me", style="C.TButton")
btn1.place(x=20, y=50)
btn2 = Button(backframe, text="Click me too", style="B.TButton")
btn2.place(x=100, y=100)
mainloop()
in 'C.TButton' styling I tried to change the background colour of the 'btn1', but it only changes border colour to blue and not the background colour. How can I change the background colour?
I'm trying to change the color of a progress bar but the :
myProgressbar.configure(background="color")
It doesn't seems to work. I searched on the site, but didn't find an answer for the case of Tkinter.
Any idea ?
Thank you.
In python 2.7, you can do the same using theme. Here is the code:-
import Tkinter as tk
import ttk as ttk
root = tk.Tk()
frame = tk.Frame(root)
frame.grid()
s = ttk.Style()
s.theme_use('clam')
s.configure("red.Horizontal.TProgressbar", foreground='red', background='red')
ttk.Progressbar(frame, style="red.Horizontal.TProgressbar", orient="horizontal", length=600,mode="determinate", maximum=4, value=1).grid(row=1, column=1)
frame.pack()
tk.mainloop()
You can take a look over this link:-
How to change ttk.progressBar color in python