Some Functions of Tkinter
Hey there!
This is second part of the Python Tkinter section.
In this blog we are coding basic structure for our calculator.
So let’s jump into coding.
You have seen the previous blog in this section there we have seen a code example after running the code you get a simple random size window in Windows you have seen a facicon there on top of that window so you don’t like like a window like that so for that purpose we are making it looks cooler than before.
What are we doing?
- Add a specific height and width to the window.
- Add a title to the window.
Code Example
Add a specific height and width to the window.
1
2
3
4
5
6
7
8
9
from tkinter import *
window = Tk()
window.geometry("1000x1000")
window.minsize(50,50)
window.maxsize(305,300)
favicon = PhotoImage(file = 'icon.png')
window.iconphoto(False, favicon)
window.title("hi")
mainloop()
Explanation
Geometry function set width and height to the window the arguments are given in string, the width and height are seperated by
x
.- Minsize defines the minimum size of the window.
- Maxsize defines the maximum size of the window.
Note : If you click on the full size button it will be full sizeed. - PhotoImage function set a file to a variable.
- Iconphoto takes first argument as false and second argument as the variable that has file name.
Note : If you directly write the file name as second argument you will get an error. - Title function sets title to the window.
All the functions above should have prefix of the variable defined for Tk()
function,
example: window.title("Hi")
Some widjets of Tkinter
Label
Label is a widget which don’t interact with user. It is just a text block.
There are basicaly three ways to create label or other widgets and thye are :
- Directly create a widget and pack or grid it at the end of the line.
- Create a variable for the widget and pack or grid it where you want to show it.
- Create a variable for the widget and pack or grid it at the end of the line.
The first and second ways are same at one angle of view they pack or grid themselves where they are defined but the difference is we can use the second way option anywhere but that’s not the case for first way.
I think the second one is better.
First option
1
Label(text="Hi there, this is a Tkinter GUI!").pack()
Second option
1
2
label=Label(text="Hi there, this is a Tkinter GUI!")
label.pack()
Third option
1
label2=Label(text="Hi there, this is a Tkinter GUI!").pack()
Here is how it works :
Explanation
- Label function defines a label. If you are includeing
*
from tkinter then you can use it directly like that but if you are importing only tkinter like thisimport tkinter
then you will need to write it something like thistk.Label("text="Hi there, this is a Tkinter GUI!")
, this is for all widgets you use. - Text defines the text of the label.
Thats all for this blog.
In next blog on this section we are learning some more attributes of the label and more widgets for our calculator app.
If you want to see all blog posts on this section goto categories and open Learn category in Tkinter category.