Tkinter root configuration - python

For adding a title to the TKinter root, we do something like:
root = Tk()
root.title("<Title Text>")
root.configure(bg = "<color>")
My question is how do we change the font size and font color of the Title Text. Seemed to me to be a simple requirement, but other than changing the background color of root, I don't seem to be able to do anything more.
Many Thanks

You can't change the title font or size via tkinter. Tkinter has no control over how the window manager / os decorates the program.

Related

How do i resize this labelframe in tkinter?

So I tried to make this labelframe wider by using the basic width and width option.
Here's my given minimal code.
from tkinter import *
from tkinter import ttk
app = Tk()
app.resizable(False, False)
mainLayout = ttk.Frame(app, padding=10)
mainLayout.grid()
settings = ttk.Labelframe(mainLayout, text="Settings", padding=10, width=1000)
settings.grid()
ttk.Label(settings, text="Length limit (in seconds)").grid()
ttk.Spinbox(settings, from_=60, to=600, width=4).grid()
app.mainloop()
minimalized preview:
used in application:
i want to get this labelframe little bit bigger and make the inside centered, But i had no knowledge to do so, Any help will apreciated!
It seems like you just want to have a main_frame in the app. For simplicity I've used .pack with the options fill and expand with the constants tkinter.BOTH to stretch the widget in both (x,y) direction and True to consume extra space. (This is one of the reasons why wildcard imports are discouraged, you can be unaware of overwriting something, use import tkinter as tk instead). Same happens with the LabelFrame, you may could delete one of the containers, but that is up to you.
In LabelFrame I have configured the grid and gave the instruction that the column 0 should get the extra space with the priority/weight 1.
In addition, I gave your Spinbox a little bit more width, changed the size of the window and separated the constructor from the geometrymethod.
To get in touch with the geometry management in tkinter, you could play around with the instructions (e.g. comment some out) and see what happens.
from tkinter import *
from tkinter import ttk
app = Tk()
app.geometry('500x500')
app.resizable(False, False)
mainLayout = ttk.Frame(app, padding=10)
mainLayout.pack(fill=BOTH,expand=True)
settings = ttk.Labelframe(mainLayout, text="Settings", padding=10, width=1000)
settings.pack(fill=BOTH,expand=True)
settings.columnconfigure(0,weight=1)
my_label = ttk.Label(settings, text="Length limit (in seconds)")
my_label.grid()
my_spinbox = ttk.Spinbox(settings, from_=60, to=600, width=20)
my_spinbox.grid()
app.mainloop()

Image does not show on button Tkinter

I'm trying to simply add an image to a tkinter button. I tried everything:
import tkinter as tk
root = tk.Tk()
root.geometry('300x300+300+150')
photo = tk.PhotoImage('home.gif')
btn = tk.Button(root, image = photo, width = 100, height = 100)
btn.image = photo # even with this does not work
btn.pack()
root.mainloop()
I also tried with PIL setting the photo variable equal to ImageTk.PhotoImage(Image.open('home.gif')), I tried easly the open function, the absolute path of the photo (and yes, the photo is inside the same directory of my script), but anything works. The window just pop up with a big button, without image inside.
UPDATE:
I tried with other images, and I noticed that some images are shown while others no. This is because the images with transparent background cause a bug or a problem to tkinter... so, I do not know if there's a way to solve this. On google I find out that some people use canvas but I actually need the image to be inside the button so I do not know how to do.
Please change your code as below
photo = tk.PhotoImage(file='home.gif')
because i changed the above code and it worked....

Make root window transparent and also other things accessible

I want to set my root window transparent, I have done this easily, but I can't move anything in the background of my system, for example my terminal, or anything else.
from tkinter import Tk
self.tk = Tk()
self.tk.attributes('-zoomed', True) # This just maximizes the window
self.tk.wait_visibility() # just to fix self.tk.attributes
self.tk.attributes('-type', 'dock') # disable title and title buttons
self.tk.attributes('-alpha', 0.1) # transparent
self.tk.mainloop() # main loop
The reason I want to do that, is because I want to make an screenshot application, and I want to make some effects that make the system transparent
You could make any color completly transparent in your window.
from tkinter import Tk
root = Tk()
root.configure(bg='white')
root.attributes('-transparentcolor', 'white')
root.attributes('-topmost', True)
root.mainloop()
I think code speaks for itself. (OS dependent, works on windows)

tkinter window not wide enough to display window title

Is there a 'standard' way of ensuring that the displayed window is wide enough to display the window title?
import tkinter as tk
root = tk.Tk()
root_f = tk.Frame(root)
root.title('Long Window Title Containing Much Text')
text_f = tk.Frame(root_f)
text_l = tk.Label(text_f, text='Short text')
root_f.grid()
text_f.grid()
text_l.grid()
root.geometry('+{}+{}'.format(100, 100))
root.mainloop()
root.quit()
When I use the winfo method to get the width of the root (or text) frames they give the size of the text not the size of a window wide enough to display the whole window title.
I know it has to be something simple, but I can't see it.
Thanks
No, there is no standard way. Tkinter has no way of knowing how long the title is on the titlebar. The OS / window manager has completely control of that portion of the window and doesn't expose any platform-independent way of getting at that information.
You would have to know not only the font used by the OS for window titles, but also whether there were any other decorations (eg: buttons, images, etc) that appear in the title area.
If you're fine with making assumptions that the font is the same as the default tkinter font, you can use tkinter's ability to measure the length of a string in a given font with the measure method of a font object.

Combobox fontsize in tkinter

Hi I am trying to use the ttk Combobox to create a dropdown with options .
While doing so i can configure the font size of the default value passed to it .
But when i click the arrow the font size of the other values remains the same .I am developing the app for touchscreen , so i need to provide proper size .
Heres the sample code , when i run the code the size of A is bigger , button the on clicking the arrow key i see the other values are of default size .
#! /usr/bin/python
from Tkinter import *
import ttk
class Application:
def __init__(self, parent):
self.parent = parent
self.combo()
def combo(self):
self.box_value = StringVar()
self.box = ttk.Combobox(self.parent, textvariable=self.box_value,font=("Helvetica",20))
self.box['values'] = ('A', 'B', 'C')
self.box.current(0)
self.box.grid(column=0, row=0)
if __name__ == '__main__':
root = Tk()
app = Application(root)
root.mainloop()
The thing is that the dropdown menu of the ttk Combobox is actually a simple Tkinter Listbox so it isn't affected by the ttk style. If it would be possible to get a reference to the Listbox from the Combobox, changing the font would be easy. However, I couldn't find a way to do so in Tkinter.
Edited as per patthoyts' very useful comment.
What you can do is change the font for all Listboxes that are part of a Combobox using
bigfont = tkFont.Font(family="Helvetica",size=20)
root.option_add("*TCombobox*Listbox*Font", bigfont)
That changes the font of all Listbox widgets that are part of a ttk Combobox and that are created after calling this.
This does affect all new Comboboxes, but I assume that's what you want. If you want the new font only for this Combobox, you could choose to create this Combobox as the last widget and call self.parent.option_add("*TCombobox*Listbox*Font", bigfont) right before creating this Combobox. Then only the Listbox under this Combobox will have the new font.
If you want all widgets to have the bigger font, you can use
root.option_add("*Font", bigfont)
or you can change the default font as described in this answer.
While working on the same issue as the OP, the problem of the arrow size mentioned in the comments of the accepted answer by Deepworks and fhdrsdg came up. Unfortunately I'm new and can't comment, hence I'm posting this as an answer. There is actually a way to set the arrow size via the Style "arrowsize" option.
style = ttk.Style()
style.configure('W.TCombobox',arrowsize = 60)
cBox = ttk.Combobox(self, style='W.TCombobox')
This allows you to increase the arrow size to match the font size of the rest of the widget.
I found the reference to the "arrowsize" option here:
Tcl8.6.10/Tk8.6.10 Documentation > Tk Commands > ttk_combobox

Categories

Resources