Tkinter

Graphical User Interface(GUI) is a form of user interface which allows users to interact with computers through visual indicators.Tkinter is used to Create Graphical User  Interface(GUI) Applications in Python. Its Gives Object Oriented Interface. Tkinter is the most widely used GUI in the world.

 

Properties Of Tkinter:
Ø Creating Windows and Dialog Boxes.
Ø Building a GUI for a Desktop Application.
Ø Adding A GUI To a Command-Line Program.
Ø Creating a Custom Widgets.
Ø
Prototyping a GUI.


Tkinter is Used to create Graphical User Interface(GUI) in Python.It’s the only framework built into the Python standard library. This framework provides Python users with a simple way to create GUI elements using the widgets found in the Tk toolkit.

Tkinter is the Python interface for Tk. Tkinter is an acronym for "Tk interface".Tk was developed as a GUI extension for the Tcl scripting language by John Ousterhout. The first release was in 1991. It is easier to learn and use than other toolkits. 

Tkinter provides the following widgets Thats Are :

button, canvas, checkbox, combobox, entry, frame, label, labelframe, listbox, menu, menubutton, message, notebook, optionMenu, panedwindow,  progressbar, radiobutton, scale, scrollbar, separator, sizegrip, spinbox, text, treeview.


Tkinter provides the following top-level windows:

chooseColor - pops up a dialog box for the user to select a color.
chooseDirectory - pops up a dialog box for the user to select a directory.
dialog - creates a modal dialog and waits for a response.
getOpenFile - pops up a dialog box for the user to select a file to open.
getSaveFile - pops up a dialog box for the user to select a file to save.
messageBox - pops up a message window and waits for a user response.
popup - posts a popup menu.
toplevel - creates and manipulates toplevel widgets.

Tkinter  also provides three geometry managers:

place - which positions widgets at absolute locations.
grid - which arranges widgets in a grid.
pack - which packs widgets into a cavity.


Here is Your First Tkinter window 

from tkinter import *

root = Tk()                   #Create Root Window, You cn use other or window.tk
root.mainloop()     #Execute Tkinter

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.

Now You Can Change Your Title Just Like Below

from tkinter import *
root = Tk() #Create Root Window, You can use other or window.tk
root.title("MY First Tkinter Window")
root.mainloop()
#Execute Tkinter

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.

Now You Can Change Background Color

from tkinter import *
root = Tk()
root.title("My First Tkinter Window")
root.configure(background=" 
#76EE00")               #Change BackgroundColor 
root.mainloop()

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.

You can use a website for color code 
https://www.webucator.com/article/python-color-constants-module/
Here You Can Find All Color code.

Now,   Where to set your output screen-----
from tkinter import *
root = Tk()
root.title("Python Window")
root.configure(background="red")
root.geometry("600x300+660+86")    #Width=600, Height=300, X-position=660,                                                                               #YPosition=86
root.mainloop()

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.



Now You Can Resize Your Window  using resizable  attributes

from tkinter import *
root = Tk()
root.title("Python Window")
root.geometry("600x300+660+86")
root.resizable(width="true", height="true") # You Can Resize Your Window width & height
root.mainloop()
OR
from tkinter import *
root = Tk()
root.title("Python Window")
root.geometry("600x300+660+86")
root.resizable(width="true", height="false") # You Can Resize Your Window width Only
root.mainloop()
OR
from tkinter import *
root = Tk()
root.title("Python Window")
root.geometry("600x300+660+86")
root.resizable(width="false", height="true") # You Can Resize Your Window height Only
root.mainloop()

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.

 To destroy a window, use its destroy method

from tkinter import *
root = Tk()
root.title("Python Window")
root.geometry("600x300+660+86")
root.destroy()                 #when Use It does not show Window that means destroy work
root.mainloop()


When your see that it running No Error Without window that means it works. 

The output screen will never fall below this width and height(400,200).

Use Of minsize(x,y) Attributes

from tkinter import *
root = Tk()
root.title("Python Window")
root.geometry("600x300+660+86")
root.resizable(width="true", height="true")
root.minsize(400,200)
root.mainloop()

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.



Q.The output screen will never Ecxeed this width and height(800,400).

Use Of maxsize(x,y) Attributes

from tkinter import *
root = Tk()
root.title("Python Window")
root.geometry("600x300+660+86")
root.resizable(width="true", height="true")
root.maxsize(800,400)
root.mainloop()

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.


Transparency In Tkinter Window:
Create A transparent window in Python Tkinter

we want to create a transparent window using tkinter. To create the transparent window, we can use the attributes property and define the opacity value.

from tkinter import *                        #Importing the tkinter library
root = Tk()                                        #Create an instance of tkinter frame
root.title("Python Window")              #Window Title
root.geometry("600x300+660+86")           #Define the size & Position of the window
root.attributes("-alpha", 0.8)       #To Make it transparent use alpha property to define the   
root.mainloop()                        #opacity of

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.


Transparentcolor Attributes:

from tkinter import *
root = Tk()
root.title("Python Window")
root.config(background="red")
root.geometry("600x300+660+86")
root.attributes('-transparentcolor', 'red')
root.mainloop()


Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.

If You want to Use a Frame in Transparentcolor

from tkinter import *
root = Tk()
root.title("Python Window")
root.config(background="black")
root.geometry("600x300+660+86")
root.attributes('-transparentcolor', 'red')
frame = Frame(root, width=200, height=200,bg="red")
frame.pack()
root.mainloop()

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.


When We Use disabled attributes

from tkinter import *
root = Tk()
root.title("Python Window")
root.config(background="red")
root.geometry("600x300+660+86")
root.attributes('-disabled', 'true') # When Use in this attributes, Output Window will be disabled
root.mainloop()

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.

When Use fullscreen attributes

from tkinter import *
root = Tk()
root.title("Python Window")
root.geometry("600x300+660+86")
root.resizable(width="true", height="true")
root.attributes('-disabled', 'false')   # When Use in this attributes, Output Window will be                                                               # disabled
root.attributes('-fullscreen', 'true')  #Window Will go full screen. Plz press Alt+F4
root.mainloop()

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.

When We Use toolwindow attributes

from tkinter import *
root = Tk()
root.title("Python Window")
root.geometry("600x300+660+86")
root.resizable(width="true", height="true")
root.attributes('-toolwindow', "true") #Output Has no Icon
root.mainloop()


Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.




Now I Will Speak Tkinter State

Tkinter has various Type of State Like Below

1. normal State :  root.state("normal")
2. zoomed State: root.state("zoomed")
3. iconify State: root.state("iconify")
4. deiconify State: root.state(deiconify)
5. withdraw  State: root.state("withdraw")


01. Example of normal State:

from tkinter import *
root = Tk()
root.title("Python Window")
root.geometry("600x300+660+86")
root.state("normal")
root.mainloop()

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.

02. Example of zoomed State:

from tkinter import *
root = Tk()
root.title("Python Window")
root.geometry("600x300+660+86")
root.state("normal")
root.state("zoomed") #window is automatically maximized
root.mainloop()

Output: Your Window is now Full Screen.

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.

03. Example of iconify State: "convert" the window to a taskbar icon and has a state of "iconic"

from tkinter import *
root = Tk()
root.title("Python Window")
root.geometry("600x300+660+86")
root.state("normal")
root.iconify()
root.mainloop()

Output: Window is go to taskbar.

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.

04. Example of deiconify State:

from tkinter import *
root = Tk()
root.title("Python Window")
root.geometry("600x300+660+86")
root.state("normal")
root.iconify()
root.deiconify()  #If you want to make the window visible again, call the deiconify method.
root.mainloop()

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.

05. Example of withdraw  State: withdraw() seems to just remove the window from the screen, after which the window has a state of "withdrawn".

from tkinter import *
root = Tk()
root.title("Python Window")
root.geometry("600x300+660+86")
root.state("normal")
root.withdraw()
root.mainloop()

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.

How to change tkinter icon

from tkinter import *
root =Tk()
root.title("Tkinter ICON Change")
root.geometry("600x350+350+86")
root.iconbitmap("E:\Python Work\ps.ico") #Icon Must be .ico format
root.mainloop()

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.



How to Change Background Window in tkinter

from tkinter import *
root =Tk()
root.title("Tkinter ICON Change")
root.geometry("600x280+350+86")
root.config(bg="#f0f022")     # change background color
root.mainloop()

Write Down Code Your Code Editor. To show Your Output. If Use Pycharm Press Shift+F10 and Show Your Output.





1 Comments

Post a Comment

Previous Post Next Post