2

I'm stuck trying to write a code in tkinter, which changes the font size when you click a radiobutton.

The code for the default text is written in the code

changeable_label = Label(the_window, text = 'Text Size' ,
    font = ('Arial' , 25), fg = 'black', width = 11, height = 2, 
       borderwidth = 1, relief = 'solid').pack()

I cant figure out a function that will change the number in 'font'. I thought of using .replace but that is for strings.

BBQ Shortcut
  • 31
  • 1
  • 1
  • 4
  • 1
    BTW, the `widget.pack()` method returns `None`, so the statement in your code does **not** store a reference to the Label widget in `changeable_label` - it stores `None`. So when you need to store a widget in a variable you need to create it & pack it in two steps, as in Anand's & my code. – PM 2Ring Sep 19 '15 at 12:21

3 Answers3

3

You can use the widget.configure() to change the font size (or other properties).

Example code of changing font based on button click -

from tkinter import *

root = Tk()

def change_font():
    changeable_label.configure(font=('Ariel',i.get()))


changeable_label = Label(root, text = 'Text Size' ,
    font = ('Arial' , 25), fg = 'black', width = 11, height = 2, 
       borderwidth = 1, relief = 'solid')
changeable_label.pack()

i = IntVar()
i.set(25)

button1 = Radiobutton(root,text = 'Increase font', variable=i, value=30, command=change_font)
button1.pack()
button2 = Radiobutton(root,text = 'Decrease font', variable=i, value=25, command=change_font)
button2.pack()

root.mainloop()

each radio button sets the appropriate value for the fontsize to the variable i , and calls the method - change_font(). In change_font() method , you can use changeable_label.configure() to change the font based on the value of variable i.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
3

As an alternative to changing the config of a widget (as in Anand's answer) you can set your widget's font using a tkFont object. Then if you change the config of the tkFont all the widgets which use that font will change. Eg

import Tkinter as tk
import tkFont

the_window = tk.Tk()

def changefont():
    label_font.config(size=12)

b = tk.Button(the_window, text="Reduce label font", command=changefont)
b.pack()

label_font = tkFont.Font(family='Arial', size=25)

for i in range(5):
    lbl = tk.Label(the_window, text='Label ' + str(i), font=label_font, 
        borderwidth=1, relief=tk.SOLID)
    lbl.pack()

the_window.mainloop()

The above code is for Python 2. In Python 3 you need to change Tkinter to tkinter, and tkFont to tkinter.font.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • That is also good. +1 . Might want to add its `tkinter.font` instead of `tkFont` for Python 3. – Anand S Kumar Sep 19 '15 at 12:19
  • Thanks for that info, @AnandSKumar. – PM 2Ring Sep 19 '15 at 12:23
  • Thanks, @BBQShortcut! Please consider [accepting](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) either my or Anand's answer, so that the system knows that your question's been answered. – PM 2Ring Sep 19 '15 at 13:23
  • I got another question. If I want the radio button to have two actions, like resizing the font but also changing the background colour, how would i modify the code so i can do this. The problem I'm having is that i cant have two 'variable' and 'command'. small_button = Radiobutton(buttons, text = 'small', variable = number, value = 15,command = change_font, variable = name_colour, colour = 'y', cmd = bg_colour ) – BBQ Shortcut Sep 21 '15 at 07:57
  • @BBQShortcut: Please ask a fresh question. You can link your new question to this one if you think that will be helpful. And it would be good if your new question contains a [mcve] that potential answerers can run & modify. – PM 2Ring Sep 21 '15 at 08:03
  • @BBQShortcut: FWIW, it's easy to give a button multiple actions in its callback command, although it can get a bit messy if you don't create your UI as a class. However, I suggest its better to not combine such different actions as changing the font size & background colour in one button. Put them into 2 separate buttons so the user can choose the combination they want. – PM 2Ring Sep 21 '15 at 08:21
1

I use this in python 3.4

from tkinter import *
L=Label(text="Hola",font=(24))
L.pack()