Friday, May 8, 2020

Create a Label widget


By: Archana Shukla And Rajesh Shukla

To add a label to our previous example, we will create a label using the label class like this:
lbl = Label(window, text=”Hello”)
Then we will set its position on the form using the grid function and give it the location like this:
lbl.grid(column=0, row=0)
So the complete code will be like this:
from tkinter import *
window = Tk()
window.title("Welcome to tKinter")
lbl = Label(window, text="Hello")
lbl.grid(column=0, row=0)
window.mainloop()


Output:
And this is the result:
Without calling the grid function for the label, it won’t show up.

Set label font size

You can set the label font so you can make it bigger and maybe bold. You can also change the font style.
To do so, you can pass the font parameter like this:
lbl = Label(window, text=”Hello”, font=(“Arial Bold”, 50))
Note: that the font parameter can be passed to any widget to change its font not labels only.
from tkinter import * window = Tk() window.title("Welcome to tkinter ") lbl = Label(window, text="Hello", font=("Arial Bold", 50)) lbl.grid(column=0, row=0) window.mainloop()



Output:



Great, but the window is so small, we cannot even see the title, what about setting the window size?


Setting window size

We can set the default window size using geometry function like this:
window.geometry(‘350×200’)
The above line sets the window width to 350 pixels and the height to 200 pixels.
On adding the above line we get the following result.

from tkinter import * window = Tk() window.title("Welcome to tkinter ") lbl = Label(window, text="Hello", font=("Arial Bold", 50)) lbl.grid(column=0, row=0) window.geometry('350x200') window.mainloop()



Output:


tk_4


Let’s try adding more GUI widgets like buttons and see how to handle button click event.



Creating our first GUI application


By: Archana Shukla And Rajesh Shukla


In order to create our first GUI application we have to import Tkinter package and create a window and set its title:

code:

from tkinter import *
window = Tk()
window.title("Welcome to tKinter")

window.mainloop()

Output:
The result will be like this:
https://pythontkintertutorial.wordpress.com/

Great!! Our application just works.
The last line which calls mainloop function, this function calls the endless loop of the window, so the window will wait for any user interaction till we close it.
If you forget to call the mainloop function, nothing will appear to the user.

Introduction to Python tKinter

By: Archana Shukla And Rajesh Shukla

Tkinter Programming

Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit.
Python has a lot of GUI frameworks, but Tkinter is the only framework that’s built into the Python standard library. Tkinter has several strengths. It’s cross-platform, so the same code works on Windows, macOS, and Linux. Visual elements are rendered using native operating system elements, so applications built with Tkinter look like they belong on the platform where they’re run.
Although Tkinter is considered the de-facto Python GUI framework, it’s not without criticism. One notable criticism is that GUIs built with Tkinter look outdated. If you want a shiny, modern interface, then Tkinter may not be what you’re looking for.
However, Tkinter is lightweight and relatively painless to use compared to other frameworks. This makes it a compelling choice for building GUI applications in Python, especially for applications where a modern sheen is unnecessary, and the top priority is to build something that’s functional and cross-platform quickly.
Tkinter package is a very powerful package. If you already have installed Python, you may use IDLE which is the integrated IDE that is shipped with Python, this IDE is written using Tkinter.
We will start by creating a window then we will learn how to add widgets such as buttons, combo boxes, etc, then we will play with their properties, so let’s get started.