How do I add guizero widgets to tkinter canvas?

229 views Asked by At

I'm trying to use tkinter canvas with guizero. I've successfully added the canvas to guizero window, but now I need to know how to add guizero widgets to that canvas. My overall goal is to have a scrollable section.

Here's my code

import tkinter as tk
from guizero import *
from PIL import Image

def file_function():
    print("File option")

def edit_function():
    print("Edit option")

def open_png():
    global level_img, picture
    path = filedialog.askopenfilename(title='choose video', defaultextension="*.png", filetypes=(('wmw level', '*.png'),('any', '*.*')))
    print(path)
    if path == '':
        return
    level_img = Image.open(path)
    picture.image = level_img

app = App(title="Hello world", layout='grid')
menubar = MenuBar(app,
                  toplevel=["File", "Edit"],
                  options=[
                      [ ["open png", open_png], ['open with selector', file_function], ["save", file_function], ['export xml', file_function], ['export png', file_function] ],
                      [ ["Edit option 1", edit_function], ["Edit option 2", edit_function] ]
                      
                  ])

objects = tk.Canvas(highlightthickness=5, highlightbackground='black')
img = tk.PhotoImage(file="image.png")
objects.create_image(0,0, image=img)
text = Text(objects, text='this is a test')
app.add_tk_widget(objects, grid=[0,0], width=200, height=300)

app.display()

I want to be able to control the canvas like a guizero box.

1

There are 1 answers

1
scotty3785 On

Rather than adding a tkinter canvas, add a guizero Drawing widget (https://lawsie.github.io/guizero/drawing/), it is basically a tk canvas with the guizero sugar sprinkled on it.

You can access a drawing widgets underlying tkinter canvas using the .tk property, for example

drawing = Drawing.app()
drawing.tk.config[bg='red']

However for your particular application given that you are using so many tkinter commands, why not just forget about guizero and use pure tkinter?