Advertisement
here2share

# Tk_ui_font_size.py

May 27th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. # Tk_ui_font_size.py
  2.  
  3. import Tkinter as tk
  4. import tkFont
  5.  
  6. UI_SIZES = ["Tiny", "Small", "Normal", "Large", "Huge"]
  7.  
  8. TINY_FONT_SIZE = 8
  9. SMALL_FONT_SIZE = 12
  10. NORMAL_FONT_SIZE = 16
  11. LARGE_FONT_SIZE = 18
  12. HUGE_FONT_SIZE = 24
  13.  
  14. root = tk.Tk()
  15.  
  16. ui_size = tk.StringVar(root, "Normal")
  17. entry_text = tk.StringVar(root, "Entry Text")
  18.  
  19.  
  20. def text_size_callback(*_args):
  21.     """change the program font size when the font_size variable changes"""
  22.     font = tkFont.nametofont("TkDefaultFont")
  23.     selected_size = ui_size.get()
  24.     if selected_size == "Tiny":
  25.         font.configure(size=TINY_FONT_SIZE)
  26.     elif selected_size == "Small":
  27.         font.configure(size=SMALL_FONT_SIZE)
  28.     elif selected_size == "Normal":
  29.         font.configure(size=NORMAL_FONT_SIZE)
  30.     elif selected_size == "Large":
  31.         font.configure(size=LARGE_FONT_SIZE)
  32.     elif selected_size == "Huge":
  33.         font.configure(size=HUGE_FONT_SIZE)
  34.     root.option_add("*Font", font)
  35.  
  36.  
  37. ui_size.trace('w', text_size_callback)
  38. ui_size.set("Normal")
  39.  
  40. ui_radio_group = tk.Frame(root)
  41. ui_size_radios = []
  42. for sizeval in UI_SIZES:
  43.     ui_size_radios.append(tk.Radiobutton(
  44.         ui_radio_group,
  45.         text=sizeval,
  46.         variable=ui_size,
  47.         value=sizeval
  48.     ))
  49. text_entry = tk.Entry(ui_radio_group, textvariable=entry_text)
  50.  
  51. i = 0
  52. for sizeradio in ui_size_radios:
  53.     sizeradio.grid(row=i, column=0, sticky=tk.W)
  54.     i += 1
  55. text_entry.grid(row=2, column=1)
  56. ui_radio_group.pack()
  57.  
  58. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement