Tkinter: How to detect screen size and change text size of Labels?
Solution 1:
For screen size:
import Tkinter as tk
x = tk.Tk()
print(x.winfo_screenwidth(), x.winfo_screenheight())
For text size:
import tkFont
...
f = tkFont.Font(size=100)
label = tk.Label(self, text='Big text', font=f)
....
To adjust text size to screen size you just need to come up with some kind of font scaling algorithm that will convert winfo_screenwidth
and winfo_screenheight
to appropriate size
value of tkFont.Font
.
Solution 2:
As was mentioned in a comment, you can use the window methods winfo_screenwidth()
& winfo_screenheight()
to find the size of your display. To change the text size for labels, simply edit the font
config option for Label
widgets.
The font
can be changed using simple string values (to change one parameter of the font, like size or font face), or it can be given a tuple
of string values to edit multiple parameters. You can see an example of how to use this, as well as the window methods, in my example:
Example:
from Tkinter import *
root = Tk()
#Center widget: Half screen dimension - half window dimension
root.geometry("350x150+%d+%d" %( ( (root.winfo_screenwidth() / 2.) - (350 / 2.) ), ( (root.winfo_screenheight() / 2.) - (150 / 2.) ) ) )
l = Label(root, font = ('consolas', '20', 'italic'), text = "This is a label", justify = CENTER).pack(pady = (50, 0))
root.mainloop()