Skip to content

Widget Windows

Luke edited this page Apr 1, 2020 · 10 revisions

Widget Windows

Creating widgets onto the canvas used to be a pain. Now, it's easier and takes just 1 method to do so. Using the provided methods also simplify the process greatly, making it more beginner-friendly. For information on tkinter widgets, take a look at effbot.org


Table of Contents

  1. Button
  2. Checkbutton
  3. Entry
  4. Frame
  5. Label
  6. LabelFrame
  7. Listbox
  8. PanedWindow
  9. RadioButton
  10. Scale
  11. Scrollbar
  12. Spinbox

Important Note:

**kwargs are automatically allocated to the correct element, i.e background will be "allocated" towards the widget while "anchor" will be allocated to the window creation


create_button()

Puts a button on the canvas

from typing import Tuple
from numbers import Real
from tkinter import Button
def create_button(self, x: Real, y: Real, **kwargs) -> Tuple[int, Button]

Example:

from CanvasPlus import CanvasPlus
from tkinter import Tk

root = Tk()
canvas = CanvasPlus(root, width=800, height=800, background = "white")
canvas.pack()

#button that prints "Hello world!" on click
canvas.create_button(
    400, 400, text = "my_button", highlightbackground = "blue", anchor = "center", command = print("Hello world!")
)

canvas.update()
canvas.mainloop()

Screen Shot 2020-03-31 at 4 33 06 PM


create_checkbutton()

Puts a checkbutton on the canvas

from numbers import Real
from typing import Tuple
from tkinter import Checkbutton
def create_checkbutton(self, x: Real, y: Real, **kwargs) -> Tuple[int, Checkbutton]

Example:

from CanvasPlus import CanvasPlus
from tkinter import Tk

root = Tk()
canvas = CanvasPlus(root, width=800, height=800, background = "white")
canvas.pack()

#checkbutton that is activated by defualt
_, checkbutton = canvas.create_checkbutton(
        400, 400, anchor = "center", bg = "brown", fg = "white", text = "My Checkbutton"
)
checkbutton.toggle()

canvas.update()
canvas.mainloop()

Screen Shot 2020-03-31 at 4 38 24 PM


create_entry()

Puts an entry box on the canvas

from numbers import Real
from typing import Tuple
from tkinter import Entry
def create_entry(self, x: Real, y: Real, **kwargs) -> Tuple[int, Entry]

Example:

from CanvasPlus import CanvasPlus
from tkinter import Tk, StringVar

root = Tk()
canvas = CanvasPlus(root, width=800, height=800, background = "white")
canvas.pack()

content = StringVar()
canvas.create_entry(400, 400, anchor = "center", textvariable = content, fg = "black", bg = "white")
content.set("a default value")

canvas.update()
canvas.mainloop()

Screen Shot 2020-03-31 at 6 20 49 PM

Getting Values from an Entry on Click

A use for the Entry and Button widgets is to submit a form when a button is clicked.

from CanvasPlus import CanvasPlus
from tkinter import Tk, StringVar

root = Tk()
canvas = CanvasPlus(root, width=800, height=800, background = "white")
canvas.pack()

#create an entry and set it's default value
content = StringVar()
canvas.create_entry(400, 400, anchor = "center", textvariable = content, fg = "black", bg = "white")
content.set("Hello, world!")

#create button to print the value in the previously created entry
canvas.create_button(
    400, 425, anchor = "center", text = "button", width = 50, highlightbackground = "green",
    command = lambda e = content: print(e.get())
)

canvas.update()
canvas.mainloop()

This implementation acheives a simillar outcome

from CanvasPlus import CanvasPlus
from tkinter import Tk, StringVar

root = Tk()
canvas = CanvasPlus(root, width=800, height=800, background = "white")
canvas.pack()

def output(var):
    print(var.get())

#create an entry and set it's default value
content = StringVar()
canvas.create_entry(400, 400, anchor = "center", textvariable = content, fg = "black", bg = "white")
content.set("Hello, world!")

#create button to print the value in the previously cretaed entry
canvas.create_button(
    400, 425, anchor = "center", text = "button", width = 50, highlightbackground = "green",
    command = lambda e = content: output(e)
)

canvas.update()
canvas.mainloop()

Screen Shot 2020-03-31 at 7 00 24 PM
When the button is clicked:
stdout:

Hello world!

create_frame()

Puts a frame widget on the canvas

from numbers import Real
from typing import Tuple
from tkinter import Frame
def create_frame(self, x: Real, y: Real, **kwargs) -> Tuple[int, Frame]

Example:

from CanvasPlus import CanvasPlus
from tkinter import Tk

root = Tk()
canvas = CanvasPlus(root, width=800, height=800, background = "white")
canvas.pack()

canvas.create_frame(
    400, 400, anchor = "center", text = "My frame", height = 100, width = 200,
    bg = "black", fg = "white", highlightbackground = "black"
)

canvas.update()
canvas.mainloop()

Screen Shot 2020-03-31 at 7 28 53 PM


create_label()

Puts a label on the canvas

from numbers import Real
from typing import Tuple
from tkinter import Label
def create_label(self, x: Real, y: Real, **kwargs) -> Tuple[int, Label]

Example:

from CanvasPlus import CanvasPlus
from tkinter import Tk

root = Tk()
canvas = CanvasPlus(root, width=800, height=800, background = "white")
canvas.pack()

canvas.create_label(
    400, 400, font = ("Times", "24"), fg = "black", bg = "green",
    text = "Hello World!", anchor = "center"
)

canvas.update()
canvas.mainloop()

Screen Shot 2020-03-31 at 7 32 20 PM


create_labelframe()

Puts a label frame on the canvas

from numbers import Real
from typing import Tuple
from tkinter import LabelFrame
def create_labelframe(self, x: Real, y: Real, **kwargs) -> Tuple[int, LabelFrame]

Example:

from CanvasPlus import CanvasPlus
from tkinter import Tk

root = Tk()
canvas = CanvasPlus(root, width=800, height=800, background = "white")
canvas.pack()

canvas.create_labelframe(
    400, 400, font = ("Times", "24"), fg = "black", bg = "green",
    anchor = "center", width = 100, height = 200
)

canvas.update()
canvas.mainloop()

Screen Shot 2020-03-31 at 7 37 02 PM


create_listbox()

Puts a listbox on the canvas

from numbers import Real
from typing import Tuple
from tkinter import Listbox
def create_listbox(self, x: Real, y: Real, **kwargs) -> Tuple[int, Listbox]

Example:

from CanvasPlus import CanvasPlus
from tkinter import Tk

root = Tk()
canvas = CanvasPlus(root, width=800, height=800, background = "white")
canvas.pack()

_, listbox = canvas.create_listbox(
    400, 400, font = ("Times", "24"), fg = "black", bg = "green",
    anchor = "center"
)

for item in ["one", "two", "three", "four"]:
    listbox.insert(-1, item)

canvas.update()
canvas.mainloop()

Screen Shot 2020-03-31 at 7 54 01 PM


create_panedwindow()

Puts ____ on the canvas

from numbers import Real
from typing import Tuple
from tkinter import PanedWindow
def create_pannedwindow(self, x: Real, y: Real, **kwargs) -> Tuple[int, PanedWindow]

Example:

from CanvasPlus import CanvasPlus
from tkinter import Tk, Label

root = Tk()
canvas = CanvasPlus(root, width=800, height=800, background = "white")
canvas.pack()

_, window = canvas.create_panedwindow(400, 400)
top = Label(window, text="top pane")
window.add(top)

bottom = Label(window, text="bottom pane")
window.add(bottom)

canvas.update()
canvas.mainloop()

Screen Shot 2020-03-31 at 8 21 44 PM


create_radiobutton()

Puts a radio button on the canvas

from numbers import Real
from typing import Tuple
from tkinter import Radiobutton
def create_radiobutton(self, x: Real, y: Real, **kwargs) -> Tuple[int, Radiobutton]

Example:

from CanvasPlus import CanvasPlus
from tkinter import Tk, StringVar

root = Tk()
canvas = CanvasPlus(root, width=800, height=800, background = "white")
canvas.pack()

counter = 0

MODES = [
    ("Option 1", "1"),
    ("Option 2", "2"),
    ("Option 3", "3"),
    ("Option 4", "4"),
]

content = StringVar()
content.set("1") # initialize

for text, mode in MODES:
    button = canvas.create_radiobutton(
        400, 400+counter*25, text = text, variable = content, value = mode, anchor = "center"
    )
    counter += 1

canvas.update()
canvas.mainloop()

Screen Shot 2020-03-31 at 8 33 15 PM


create_scale()

Puts scale on the canvas

from numbers import Real
from typing import Tuple
from tkinter import Scale
def create_scale(self, x: Real, y: Real, **kwargs) -> Tuple[int, Scale]

Example:

from CanvasPlus import CanvasPlus
from tkinter import Tk

root = Tk()
canvas = CanvasPlus(root, width=800, height=800, background = "white")
canvas.pack()

canvas.create_scale(
    400, 400, anchor = "center", from_ = 0, to = 100,
    bg = "black", fg = "white"
)

canvas.update()
canvas.mainloop()

screen_recording


create_scrollbar()

Puts a scrollbar on the canvas

from numbers import Real
from typing import Tuple
from tkinter import Scrollbar
def create_scrollbar(self, x: Real, y: Real, **kwargs) -> Tuple[int, Scrollbar]

Example:

from CanvasPlus import CanvasPlus
from tkinter import Tk

root = Tk()
canvas = CanvasPlus(root, width=800, height=800, background = "white")
canvas.pack()

canvas.create_scrollbar(400, 400, anchor = "center", bg = "black")

canvas.update()
canvas.mainloop()

Screen Shot 2020-03-31 at 8 47 05 PM


create_spinbox()

Puts a spinbox on the canvas

from numbers import Real
from typing import Tuple
from tkinter import Spinbox
def create_spinbox(self, x: Real, y: Real, **kwargs) -> Tuple[int, Spinbox]

Example:

from CanvasPlus import CanvasPlus
from tkinter import Tk

root = Tk()
canvas = CanvasPlus(root, width=800, height=800, background = "white")
canvas.pack()

canvas.create_spinbox(400, 400, from_ = 0, to = 100)

canvas.update()
canvas.mainloop()

Screen Shot 2020-03-31 at 8 50 41 PM

Clone this wiki locally