Python font size tkinter

Python tkinter label – How to use

Are you trying to learn Python GUI programming? Let us check out in detail the Python Tkinter label. How to use Label in Python Tkinter. We will cover all the topics as :

  • About Tkinter label
  • Tkinter label font size
  • Tkinter label position
  • Tkinter label border
  • Tkinter label transparent background
  • Tkinter label attributes
  • Tkinter label image
  • Tkinter label attributes
  • Tkinter label image
  • Tkinter label text-align left

Python Tkinter label

Let us see what is a Python Tkinter label?

  • The label simply means the text on the screen.
  • It could be an instruction or information.
  • Labels are the widely used widget & is a command in all the GUI supporting tools & languages.
  • Labels are also used to display images & icons.
  • Few popular label options are:
    • text: to display text
    • textvariable: specifies name who will replace text. Mostly used when text is changing.
    • underline: underlines just one character of the text string
    • width: adds addition width to label, takes a number as an argument.
    • image: to add an image or to style the label. It uses style.map()
    • compound: display images with text, also provide control to position them.

    Tkinter label font size

    Let us see how to set font size in Python Tkinter label.

    • Font-size creates emphasis on user.
    • It makes things more clear & readable.
    • In label font size can be controlled using keyword font

    In this syntax ws is the master that we are using, font-name could be any font supported by ms word like Arial, Times new roman, Calibri, etc. weight can be Bold, italic, underline.

    Label(ws, text="any text here", font=('font-name & weight', 'font-size')
    from tkinter import * ws = Tk() Label(ws, text="Hello there!", font=("arial italic", 18) ).pack() ws.mainloop()

    So in this output, you can see that the text “Hello there!” have size 18 and is italic. Similarly, you can put bold and underline.

    Python tkinter label

    Tkinter label position

    • The right placement of widgets can create a difference.
    • Label position can be controlled using pack, grid & place
    • refer to our geometry positioning section to know more about them
    Label(ws, text="any text", font=(14, "roboto")).pack()
    Label(ws, text="any text", font=(14, "roboto")).grid(row=value, columns=value)
    Label(ws, text="any text", font=(14, "roboto")).place(x=coordinate_value, y=coordinate_value)

    Tkinter label border

    • The Python Tkinter Label border defines the type of border & its thickness.
    • There 6 types of borders each having their on property:
      • Flat
      • Raised
      • Sunken
      • ridge
      • solid
      • groove

      Syntax: borderwidth should be provided any integer value, relief should provide anyone out of these ( flat, raised, sunken, ridge, solid, groove).

      Label(ws, text="any text", borderwidth=value, relief="typeofborder").pack()
      from tkinter import * ws = Tk() ws.title("Border") ws.geometry("300x400") Label(ws, text="Flat border", borderwidth=3, relief="flat", padx=5, pady=10).pack(padx=5, pady=10) Label(ws, text="raised border", borderwidth=3, relief="raised", padx=5, pady=10).pack(padx=5, pady=10) Label(ws, text="sunken border", borderwidth=3, relief="sunken", padx=5, pady=10).pack(padx=5, pady=10) Label(ws, text="ridge border", borderwidth=3, relief="ridge", padx=5, pady=10).pack(padx=5, pady=10) Label(ws, text="solid border", borderwidth=3, relief="solid", padx=5, pady=10).pack(padx=5, pady=10) Label(ws, text="groove border", borderwidth=3, relief="groove",padx=5, pady=10).pack(padx=5, pady=10) ws.mainloop() 

      In this output, all types of border are displayed, each have width of 3 pixels. padx & pady determines the extra space around the box.

      Python Label border width

      Tkinter label transparent background

      • Transparent background means you can see through the frame.
      • only an image will appear the rest frame will be invisible.
      • make sure to take an image that does not have the background
      • to provide bg color to the image and assign the same color to wm_attributes command.
      • we tried in all colors but grey works best.

      This colourname should be same as the image bg color. We tried with many colors but grey works best for this.

      ws.wm_attributes("-transparentcolor", 'colorname') 
      from tkinter import * ws = Tk() giant=PhotoImage(file='bg.png') Label(ws,image=giant,bg='grey').pack() ws.wm_attributes("-transparentcolor", 'grey') ws.mainloop()

      In this output, picture of giant has been displayed with no background. You can see through the image frame.

      Python Label transparent background

      Tkinter label attributes

      • Attributes refer to features of the label
      • They are also called options that facilitate more functions
      • though we have discussed most of them in this blog.
      • but we are summarising all of them here.
      • Attributes are as following:
      1. colours:
      • colours plays an important role in software development.
      • Every colours has some meaning like red for error, green of correct, etc.
      • bg is used to fill background colour of the label
      • fg is used to change the text colour.
      • you can either provide a colour name or hex code
      Label(ws, text="colors", bg="blue", fg="#000").pack()

      2. font :

      • fonts make text readable.
      • to know more about fonts
      • Please refer to our Tkinter label font size section
      Label(ws, text="font demo", font=('arial bold', 18)).pack()
      • relief is used to provide decoration to the border.
      • It has various options that can be used to emphasise text.
      • To know more about options check Tkinter label border section.
      Label(ws, text="relief demo", borderwidth=3, relief='solid border').pack()
      • It used to change the cursor image when hovered over the label.
      • here is the list of all cursors that one can use
      • arrow, circle, clock, dotbox, exchange, fleur, heart man, mouse, pirate, pls, shuttle, sizing, spider, spraycan, star, target, tcross, trek, watch
      Label(ws, text="cursor demo", cursor="watch").pack()

      5. Height & Width:

      • Height determines the vertical dimension of the label.
      • width determines the horizontal dimension of the label
      Label(ws, text="Height & width", height=5, width=10).pack()

      6. padx & pady

      • padx adds empty space vertically
      • pady adds empty space horizontally
      • if they are used with in the label then they add extra space inside the box
      • if they are used in the positioning section (pack/grid/place) then add space outside the box.

      Example: Inside the box

      Label(ws, text="padding demo", padx=10, pady=5).pack()

      7. Justify:

      • Justify is used for alignment.
      • It works similar to anchor but has only three options
      • LEFT, CENTER, RIGHT
      • programmers prefer to use the anchor as it gives more control
      Label(ws, text="padding demo", justify=CENTER).pack()
      • Anchor also provides controls for alignment.
      • to know more about it refer to our section Tkinter label text-alignment
      Label(ws, text="padding demo").pack(anchor='w')

      So these were popular attributes for Label. Most of them work in other widgets as well as entry, button, etc. Now before we end this section, here is the program with attributes used in it. The controls are placed in a sequence for your convince. Do notice each and every attribute used. cursor one is my favorite

      from tkinter import * ws = Tk() ws.title("pythonguides") ws.geometry('200x150') Label(ws, text='danger', bg='red', fg='red', font=('Arial bold', 14), relief='raised', cursor='pirate', height=4, width=10, padx=10, pady=10, justify=CENTER).pack(padx=10, pady=10, anchor='w') ws.mainloop()

      This is a simple program created using all the attributes mentioned above. Few highlights, the cursor has transforming ability to the pirate icon, though it is pack() still it is in the corner because of anchor set to west, blue is bg & red text is fg. Text is justified to center, etc.

      Python Label attributes

      Tkinter label image

      • Images make things interesting.
      • In this section we will learn how to put image in label
      • We will also learn to place text on image.
      var-name = PhotoImage(file="path/of/image.extension") Label(ws, image=var-name).pack() 
      from tkinter import * ws = Tk() ws.title("giant image") giant = PhotoImage(file="bg.png") Label(ws, text="hi",image=giant).pack(side="top") intro = '''Hi! I am Giant from COC, I have been programmed to break walls & bones.''' Label(ws, text=intro, anchor=S, font=('Arial bold', 14), underline=9).pack(side="bottom") ws.mainloop()

      In this output, giant image has been placed, below the image there is a brief information about him. In the intro section G is underlined and for that we have used underline option in label.

      Tkinter label image

      Tkinter label text-alignment

      Syntax: In place of direction provide any one out of these (SN, SE, W, NE, SE, SW, CENTER)

      Label(ws, text="sometext", anchor= direction).pack()
      from tkinter import * ws = Tk() ws.title("Border") ws.geometry("50x100") Label(ws, text="Left", anchor=W).pack(fill='both') Label(ws, text="Right", anchor=E).pack(fill='both') ws.mainloop() 

      In this output, you can see that text has been aligned to left and right using anchor.

      Python Label text-alignment

      You may like the following Python tutorials:

      • Increment and Decrement operators in Python
      • Python Anonymous Function (Lambda Function)
      • Python access modifiers + Examples
      • Python Read CSV File and Write CSV File
      • Python Array with Examples
      • Hash table in python
      • Block Indentation in Python
      • Python get filename from the path
      • Python TypeError: ‘list’ object is not callable
      • Python Tkinter Entry – How to use
      • Python Tkinter Button – How to use
      • Python Tkinter Menu bar – How to Use
      • Python Tkinter Checkbutton – How to use
      • Python Tkinter radiobutton – How to use
      • Python Tkinter Calculator
      • Python Tkinter Canvas Tutorial

      In this tutorial we have learned Python tkinter label.

      • What is the Tkinter label in Python
      • Tkinter label font size
      • Tkinter label position
      • Tkinter label border
      • Tkinter label transparent background
      • Tkinter label attributes
      • Tkinter label image
      • Tkinter label attributes
      • Tkinter label image
      • Tkinter label text-align left

      I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.

      Источник

      Читайте также:  Https wr1 fsin service ru letter censor auth login html
Оцените статью