Commit 24910c5b authored by JEAN-YVES SGRO's avatar JEAN-YVES SGRO
Browse files

Update 2023-Spring/2023-02-28-session-04/GUI_02-tkinter.ipynb

parent 60eec3a4
Loading
Loading
Loading
Loading
+84 −0
Original line number Diff line number Diff line
%% Cell type:markdown id:633b41d1-434c-4ee6-ad49-c2b06be8811c tags:

# Using `tkinter` with `guizero`
https://lawsie.github.io/guizero/usingtk/

%% Cell type:code id:d6f819e6-8a56-4561-9106-a7a52bfda3b9 tags:

``` python
# Install tkinter:
# Run in shell:
# !pip install tk
```

%% Cell type:markdown id:f128b66b-3402-4e83-8f51-0f39d010d17e tags:

## Using tkinter

If you are an advanced user, you can still make use of tkinter when using `guizero`.

You can combine the use of `guizero` and `tkinter` seamlessly in a program, taking advantage of the simplified syntax of guizero whilst still being able to access the full range of functionality in `tkinter` if you need it.

### Using tkinter widgets in guizero

You can add tk widgets into your guizero app using the `add_tk_widget` method of `App`, `Window` and `Box`.

In this example, we are adding the tkinter widget `Spinbox` into a guizero `App`:


%% Cell type:code id:bef00d04-9bdc-46ca-912f-3bbcef7ca76b tags:

``` python
from guizero import App, Text
from tkinter import Spinbox

app = App()
text = Text(app, text="A Spinbox widget")

spinbox = Spinbox(from_=0, to=10)
app.add_tk_widget(spinbox)

app.display()
```

%% Cell type:markdown id:60f29684-1099-4a99-8128-30609aa50427 tags:

When adding a tk widget to a `Box` or a `Window` you will have to specify its tk property when creating the tk widget.

%% Cell type:code id:1ca2ce78-d771-48b7-8e58-68a459427a6b tags:

``` python
from guizero import App, Text, Box
from tkinter import Spinbox

app = App()
text = Text(app, text="A BOX widget")

box = Box(app)
spinbox = Spinbox(box.tk, from_=0, to=10)
box.add_tk_widget(spinbox)

app.display()
```

%% Cell type:markdown id:c9d17d5d-d85f-444b-a689-03466e41dd5b tags:

### Using a tkinter method on a guizero object
Each guizero widget itself contains a tk widget - you can find out which by looking on the guizero documentation page for the widget. For example, a guizero `TextBox` contains a tkinter `Entry` object. You can access the internal object using the syntax `<object_name>.tk`.

In this example, we have guizero `App` and `TextBox` widgets and are using the tk widgets `config` method to change the mouse cursor when it is over the `TextBox`.

%% Cell type:code id:34ef3316-f066-4eb0-a24d-6f3b9a4e3b73 tags:

``` python
from guizero import App, TextBox
app = App()
name = TextBox(app, text="Laura")
name.tk.config(cursor="target")
app.display()
```

%% Cell type:code id:8fc005a2-4aa4-4bb6-9da4-3c7b0ea79f89 tags:

``` python
```