-
Notifications
You must be signed in to change notification settings - Fork 1
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
- Button
- Checkbutton
- Entry
- Frame
- Label
- LabelFrame
- Listbox
- PanedWindow
- RadioButton
- Scale
- Scrollbar
- Spinbox
**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
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]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()
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]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()
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]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()
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()
When the button is clicked:
stdout:
Hello world!
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]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()
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]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()
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]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()
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]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()
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]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()
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]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()
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]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()
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]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()
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]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()
Copyright (C) 2020 Luke Zhang
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the conditions at https://github.com/Luke-zhang-04/CanvasPlus/blob/master/LICENSE.