Advertisement
Uno-Dan

Tkinter how to size fonts and keep alignment

Nov 14th, 2019
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 KB | None | 0 0
  1. import tkinter as tk
  2. import tkinter.ttk as ttk
  3. import tkinter.font as tkfont
  4.  
  5.  
  6. root = tk.Tk()
  7. root.rowconfigure(0, weight=1)
  8. root.columnconfigure(99, weight=1)
  9. style = ttk.Style(root)
  10.  
  11.  
  12. def resize(ev):
  13.     wdg = ev.widget
  14.  
  15.     def func():
  16.         size = wdg.get()  # get current spinbox's value
  17.         font1.configure(size=size)
  18.         font2.configure(size=size)
  19.  
  20.         for e in entries:  # update font in all entries
  21.             e.configure(font=font2)
  22.  
  23.     root.after(0, func)
  24.  
  25.  
  26. style.layout(
  27.     'resize1.TSpinbox', [(
  28.         'Spinbox.field', {
  29.             'expand': 1,
  30.             'sticky': tk.NSEW,
  31.             'children': [(
  32.                 'null', {
  33.                     'side': 'right',
  34.                     'sticky': 'ns',
  35.                     'children': [(
  36.                         'Spinbox.uparrow', {
  37.                             'side': 'top', 'sticky': 'e'
  38.                         }
  39.                     ), (
  40.                         'Spinbox.downarrow', {
  41.                             'side': 'bottom', 'sticky': 'e'
  42.                         }
  43.                     )]
  44.                 }
  45.             ), (
  46.                 'Spinbox.padding', {
  47.                     'sticky': tk.NSEW,
  48.                     'children': [(
  49.                         'Spinbox.textarea', {
  50.                             'sticky': tk.NSEW
  51.                         }
  52.                     )]
  53.                 }
  54.             )]
  55.         })
  56.     ])
  57.  
  58. style.layout(
  59.     'resize2.TSpinbox', [(
  60.         'Spinbox.field', {
  61.             'expand': 1,
  62.             'sticky': tk.NSEW,
  63.             'children': [(
  64.                 'null', {
  65.                     'side': 'right',
  66.                     'sticky': 'e',
  67.                     'children': [(
  68.                         'Spinbox.uparrow', {
  69.                             'side': 'top', 'sticky': 'e'
  70.                         }
  71.                     ), (
  72.                         'Spinbox.downarrow', {
  73.                             'side': 'bottom',  'sticky': 'e'
  74.                         }
  75.                     )]
  76.                 }
  77.             ), (
  78.                 'Spinbox.padding', {
  79.                     'sticky': tk.NSEW,
  80.                     'children': [(
  81.                         'Spinbox.textarea', {
  82.                             'sticky': tk.NSEW
  83.                         }
  84.                     )]
  85.                 }
  86.             )]
  87.         })
  88.     ])
  89.  
  90. frame = ttk.Frame(root)
  91. frame.rowconfigure(0, weight=1)
  92. frame.columnconfigure(0, weight=1)
  93.  
  94. entries = []  # list of all entries
  95.  
  96. font1 = tkfont.nametofont('TkDefaultFont')
  97. font2 = tkfont.nametofont('TkTextFont')
  98.  
  99. cbo = ttk.Combobox(frame)
  100. entries.append(cbo)
  101. cbo.config(values=('Test 1', 'Test 2', 'Test 3'))
  102. cbo.set(value='Test 1')
  103.  
  104. ent_var = tk.StringVar()
  105. ent = ttk.Entry(frame, textvariable=ent_var)
  106. entries.append(ent)
  107. ent_var.set('Testing')
  108.  
  109. spn_var = tk.IntVar()
  110. spn_var.set(font1.actual()['size'])
  111. spn = ttk.Spinbox(frame, textvariable=spn_var, values=tuple(range(1, 101)), style='resize1.TSpinbox')
  112. entries.append(spn)
  113. spn.bind('<<Increment>>', resize)
  114. spn.bind('<<Decrement>>', resize)
  115.  
  116. spn2_var = tk.IntVar()
  117. spn2_var.set(font1.actual()['size'])
  118. spn2 = ttk.Spinbox(frame, textvariable=spn2_var, values=tuple(range(1, 101)), style='resize2.TSpinbox')
  119. entries.append(spn2)
  120. spn2.bind('<<Increment>>', resize)
  121. spn2.bind('<<Decrement>>', resize)
  122.  
  123. lbl_field_name = ttk.Label(frame, text='Field Name')
  124. lb_size = ttk.Label(frame, text='Font Size')
  125.  
  126. lbl_field_name.grid()
  127. cbo.grid(row=0, column=1, sticky=tk.NSEW)
  128. ent.grid(row=0, column=2, sticky=tk.NSEW)
  129. lb_size.grid(row=0, column=3, sticky=tk.NSEW)
  130. spn.grid(row=0, column=4, sticky=tk.NSEW)
  131. spn2.grid(row=0, column=5, sticky=tk.NSEW)
  132. frame.grid(sticky=tk.NSEW)
  133.  
  134. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement