PYTHON ----> TKINTER NOTES
Click for new version of tkinter notes
IMPORTING TKINTER
import tkinter
# ANOTHER WAY (USED IN THIS PAGE)
from tkinter import *
ADDITIONAL IMPORT STATEMENTS
from tkinter.ttk import *
NECESSARY CODE
root = Tk() root.mainloop()
WIDGETS IN TKINTER
- Label
- Text
- Frame
- Canvas
myLabel = Label(master , **options) # TO KNOW ABOUT **options RUN # help(Label) # in the terminal
myText = Text(master , **options) # TO KNOW ABOUT **options RUN # help(Text) # in the terminal
myFrame = Frame(master , **options) # TO KNOW ABOUT **options RUN # help(Frame) # in the terminal
myCanvas = Canvas(master , **options) # TO KNOW ABOUT **options RUN # help(Canvas) # in the terminal
MENU IN TKINTER
FILES IN TKINTER
1.) IMPORT FILEDIALOG ---> FIRST STEP
from tkinter import filedialog
2.) UNDERSTANDING FILETYPES
- CODE
- DESC
fileType = [('PYTHON FILE' , '*.py') , ('ALL FILES','*.*')]
fileType defines filetypes which can be saved or opened fileType must be a list elements in fileType must be tuple element[0] = 'NAME OF FILE TYPE (ANY NAME YOU WANT TO GIVE)' element[1] = 'NameOfFile.FileExtension' for ex:- ("PYTHON FILES" , "*.py") * represents any value for ex:- ("ALL FILES" , "*.*") *.* any name of file with any extension can be opened or saved.
3.) OPENING A FILE DIALOG OPTION
file = filedialog.askopenfile(mode = mode('w','r'...etc) , filetypes = fileType) # returns open file(selectedFile)
4.) SAVING A FILE DIALOG OPTION
file = filedialog.asksaveasfile(mode = mode('w','r'...etc) , filetypes = fileType) # returns open file(savedFile)
TEXT WIDGET IN TKINTER
1.) MAKING A TEXT WIDGET
myTextBox = Text(master , **options)
2.) TAGS IN TEXT WIDGET
- CODE
- DESC
myTextBox.tag_add(tagName, startIndex , endIndex) myTextBox.tag_config(tagName , **options) #TO KNOW MORE ABOUT TEXT: #https://www.geeksforgeeks.org/python-tkinter-text-widget/
You can create a tag and use tag_config to apply specific options (bg , fg ,etc) on that particular tagRegion(startIndex , endIndex)
3.) MARKS IN TKINTER
- CODE
- DESC
#ADDING A MARK myTextBox.mark_set(markName , IndexToPlace) #SETTING GRAVITY OF A MARK myTextBox.mark_gravity(markName , LEFT/RIGHT/"left"/"right")
Marks are like tags . The only difference is that it is not specified for a region , but for a specific point . mark_gravity is used to set the direction of the insertion cursor where the character will be inserted. if direction is at left and insertion cursor is at the mark , then the new character will be inserted in left direction and same happens with the right direction BY DEFAULT IT IS RIGHT
4.) INSERTOFFTIME/INSERTONTIME
You can use :- insertontime insertofftime These prorperties are used to set the time for the insertion cursor for which it will blink insertontime --> cursor will appear for this time default value => 300 insertofftime --> cursor will not appear for this time default value => 600
EVENTS IN TKINTER
- KEY PRESS
- KEY RELEASE
- MOUSE ENTERED
- MOUSE LEFT
# LET US BIND A TEXT WIDGET TO KEY PRESS EVENT textWidget.bind("<KeyPress>",nameOfFunction) # TO GET THE KEY PRESSED def nameOfFunction(event): keyPressed = event.char
Widget.bind("<KeyRelease>",nameOfFunction)
Widget.bind("<Enter>",nameOfFunction)
Widget.bind("<Leave>",nameOfFunction)
Comments
Post a Comment