|

Hints on Using Tkinter

Installing Tkinter on Linux

sudo apt-get install python3-tk

Tkinter Options

  • Frame Border
    • highlightbackground = “enter color here” (puts a border with your chosen color around the frame)
    • highlightthickness = 20 (changes the thickness of the border)
  • PhotoImage(file = “____”).subsample(x,y) ==> shrinks photo depending on the numbers you put in x and y, for example 10 and 10
  • PhotoImage(file = “____”).zoom(x,y) ==> enlarges photo depending on the numbers you put in x and y
  • pack()
    • side = LEFT, RIGHT, TOP, BOTTOM
    • fill = x, y, BOTH
    • padx= , pady= (external)
    • ipadx= , ipady= (internal)
  • grid()
    • row, column
    • padx= , pady= (external)
    • columnspan
    • rowspan
  • place()
    • x= , y= coordinates
    • fill, relx, rely, padx, etc.
  • Treeview
    • style = ttk.Style()
    • style.configure(“Treeview:
      • background = “color”,
      • foreground = “color”,
      • rowheight = 25,
      • fieldbackground = “color”)
    • style.map(‘Treeview’)
    • style.theme_use(“clam or default or alt”)
  • Copying information to the main clipboard with Tkinter Where ‘root’ is the main window
    • root.clipboard_clear()
    • root.clipboard_append()
    • root.clipboard_get()
  • Bind
    • tkinterobject.bind(event, action)
    • tkinterobject.bind(“<event>”, command)
      • Button-1 (left mouse button) and Button-3 (right mouse button)
      • Enter, Leave, Focus
      • Return (main enter key)
      • KP_Enter (keypad enter key)
      • myButton.bind(“<Leave>”, clicker) #On loss of focus#
      • myButton.bind(“<FocusIn>”, clicker) #Triggers event on tab into button
      • myButton.bind(“<Enter>”, clicker) #Triggers button when hover over it
      • myButton.bind(“<FocusOut>”, clicker) #Triggers on loss of focus
      • myButton.bind(“<Return>”, clicker) #Left Click
      • myButton.bind(“<Key>”, clicker) #Focus on button and hit on a key
  • tkinterobject.bind(“<Key>”, command) == put event.keysym in the command to find the name of the key you pressed
  • windows to top and background
    • window.lower() #puts the tkinter window (root) into the background
    • window.lift() #puts the tkinter window (root) into the foreground
    • window.attributes(‘-topmost’,True) #keeps window on top at all times
  • getting help information on tkinter objects
    • my_help = str(help(________)) #where you fill in the blank with the tkinter object, example: Label, Entry, etc.
    • print(my_help) #will print the help information on the object to the terminal

Useful Tkinter Links

Tkinter Widget Book

Here’s a link to your free pdf copy of my new Tkinter Widget Quick ReferenceGuide Book!
https://tkinter.com/widget-book-download/
There you can view the book, or download the book to your computer or phone or tablet or whatever.
John Elder, Founder – Tkinter.com