Skip to content

Widget Windows

Luke edited this page Mar 31, 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.


Table of Contents

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

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
For more on tkinter button, go to https://effbot.org/tkinterbook/button.htm


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
For more on tkinter checkbutton, go to https://effbot.org/tkinterbook/checkbutton.htm


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()

create_label()

create_labelframe()

create_listbox()

create_menu()

create_panedwindow()

create_radiobutton()

create_scale()

create_scrollbar()

create_spinbox()

create_text_widget()

create_toplevel()

Clone this wiki locally