How to Set a Font for the Options Menu

How to change the font of Action Bar's Option Menu Items Title?

Inside in styles.xml add below code

<item name="android:actionMenuTextAppearance">@style/MyActionBar.MenuTextStyle</item>

so the styles.xml look like this.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

<item name="android:itemBackground">@color/skyBlue</item>
<item name="android:textStyle">bold</item>
<item name="android:actionMenuTextAppearance">@style/MyActionBar.MenuTextStyle</item>
</style>

<style name="MyActionBar.MenuTextStyle" parent="Theme.AppCompat">
<item name="actionMenuTextColor">@color/white</item>
<item name="android:actionMenuTextColor">@color/white</item>

</style>

and then create style for MyActionBar.MenuTextStyle and change the TextView property as you wish.

Note : You can only use custom fonts via Java code, not through
layout XML or styles/themes.

for more detail visit this : Set specific font in a styles.xml

Tkinter OptionMenu: How to configure font size of drop down list?

You have to use the nametowidget() method to get the widget object corresponding to the dropdown menu widget, then set its configuration.

Here's a runnable example:

import tkinter as tk
import tkinter.font as tkFont

root = tk.Tk()
root.geometry('300x200')

helv36 = tkFont.Font(family='Helvetica', size=36)
options = 'eggs spam toast'.split()
selected = tk.StringVar(root, value=options[0])

choose_test = tk.OptionMenu(root, selected, *options)
choose_test.config(font=helv36) # set the button font

helv20 = tkFont.Font(family='Helvetica', size=20)
menu = root.nametowidget(choose_test.menuname) # Get menu widget.
menu.config(font=helv20) # Set the dropdown menu's font
choose_test.grid(row=0, column=0, sticky='nsew')

root.mainloop()

Here's are two screenshots showing the default vs modified dropdown menu text sizes:

screenshot of dropdown menus

Is it possible to change the font of every menu option in Tkinter

Yes, it is possible to change the font of each item in an option menu but make sure that font is compatible with Tkinter otherwise it won't show up, you can check all the fonts compatible with Tkinter by

import tkinter as tk
import tkinter.font as font

root = tk.Tk()
print(list(font.families()))

Now to change each font in the optionmenu we use entryconfigure method of the menu linked with the optionmenu widget can be accessed by its object (op['menu']). The menu of optionmenu has all the options of a Tkinter Menu.

Take a look at this example.

import tkinter as tk
import tkinter.font as font

root = tk.Tk()
root.geometry('200x150')
var = tk.StringVar()

op = tk.OptionMenu(root, var, *font.families())
op.pack(pady=20)

lb = tk.Label(root, textvariable=var)
lb.pack()

for item in range(0, int(op['menu'].index('end'))):
op['menu'].entryconfig(item, font=font.Font(family=font.families()[item]))

var.trace('w', lambda *a: lb.config(font=op['menu'].entrycget(var.get(),'font')))
root.mainloop()


Related Topics



Leave a reply



Submit