Skip to main content

PYTHON - TKINTER NOTES

   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


1.) CREATING A MENU

menuBar = Menu(master ,**options)


2.) ADDING TO MENU

menuOption = Menu(menuBar ,tearoff=0)
menuOption.add_command(label=nameOfOption ,command=commandName)
menuBar.add_cascade(label=nameOfMenuOption , menu=menuOption)


3.) ADDING MENU TO MASTER

master/root.config(menu = menuBar)


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

Popular posts from this blog

PYTHON - PYGAME NOTES

PYTHON PYGAME NOTES                     ----->  FOR YOU Click for newer version of pygame notes (better) BASIC  PYGAME  STARTING :- import pygame pygame. init() screen = pygame.display. set_mode ( ( WIDTH , HEIGHT ) ) BASIC REQUIRED CODE :- There must be following code to get a screen running = True while running : for event in pygame.event. get (): if event.type == QUIT : running = False pygame.display. update () DRAWING WITH PYGAME SHOW MORE pygame.draw. rect ( surface ,( r,g,b ),( x-coordinate , y-coordinate , width , height )) # Doing the same thing with pygame.Rect rect = pygame. rect ( x-coordinate , y-coordinate , width , height ) pygame.draw. rect ( surface ,( r,g,b ), rect ) # RESIZING A RECTANGLE rect. inflate ( width , height ) # if you give -ve width or height the recta...

PYTHON -- UTILITIES

BASIC PYTHON UTILITIES       AND SOME BASIC MODULES Click the link for newer and better version of notes SOME BASIC PYTHON FUNCTIONS AND MODULES  WHICH CAN BE AT TIMES VERY USEFUL KEYBOARD MODULE SHOW MORE pip install keyboard import keyboard # TYPES THE INPUT TEXT THROUGH KEYBOARD keyboard. write ( INPUT ) # TO CHECK IF A KEY IS PRESSED ON KEYBOARD keyboard. is_pressed ( KEY_NAME ) # returns True if keyprssed else false , # can be used with while loop OS MODULE IN PYTHON SHOW MORE IMPORTING OS MODULE import os DIR RELATED FUNCTIONS mkDIR rmDIR listDIR currentDir changeCWD checkIfDir os. mkdir ( nameOfDIr ) os. rmdir ( nameOfDIr ) os. listdir () # DIR_PATH (OPTIONAL ARGUMENT) WHICH CAN BE GIVEN # TO GET THE DIRS/FILES INSIDE THE DIR_PATH # ELSE THE DIRS IN CURRENT_WORKING_DIRECTORY WILL BE GIVE...