Scaling a tkinter project to fit 320x240 raspberry Pi screen Scaling a tkinter project to fit 320x240 raspberry Pi screen tkinter tkinter

Scaling a tkinter project to fit 320x240 raspberry Pi screen


You can have a background image without using a Canvas widget and doing so will allow you to use tkinter's geometry managers to place your widgets. I don't really understand the relationship between the Raspberry Pi's 320x240 screen and the 1280x480 HDMI one.

The code below illustrates how to display a background image and some widgets on top of it. There's also a Button to toggle the window's size between the two you want.

from PIL import Image, ImageTktry:    import Tkinter as tkexcept:    import tkinter as tkpath_to_bkgr_img = "redpoly2.jpg"WIN_SIZES = (320, 240), (1280, 480)# Translates an rgb tuple of int to a tkinter friendly color code.def _from_rgb(rgb):    return "#%02x%02x%02x" % rgbdef change_size():    """ Sets/changes window size to next one available in WIN_SIZES. """    global cur_size    cur_size = (cur_size + 1) % len(WIN_SIZES)    config_window()def config_window():    """ Sets root window's title, size, and background image. """    global background_label    geometry = '{}x{}'.format(*WIN_SIZES[cur_size])    root.geometry(geometry)    root.title(geometry)    # Resize background to fit window size.    btn_img = background_image.resize(WIN_SIZES[cur_size], resample=Image.BICUBIC)    btn_img = ImageTk.PhotoImage(btn_img)  # Make tkinter compatible.    if not background_label:  # Create Label if necessary.        background_label = tk.Label(root)    background_label.config(image=btn_img)    background_label.image = btn_img  # Keep reference.    background_label.place(x=0, y=0, relwidth=1, relheight=1)root = tk.Tk()background_image = Image.open(path_to_bkgr_img)background_label = Nonecur_size = 0config_window()titleLabel = tk.Label(root, fg="white", text="TEXT", borderwidth=2, relief="solid",                      bg=_from_rgb((239, 36, 37)), font=("Courier", 44))titleLabel.pack(padx=5, pady=5, expand=1)logButton = tk.Button(root, fg="white", text="Change Size", command=change_size,                      borderwidth=2, relief="raised", bg=_from_rgb((239, 36, 37)),                      font=("Courier", 22))logButton.pack(padx=5, pady=5, expand=1)root.bind_all('<KeyPress-Escape>', lambda *event: quit())  # Press Esc key to quit app.root.mainloop()

Here are screenshots showing the what's displayed for each size:

screenshot of small window

screenshot of large window


The output of the RPi can be configured within the config.txt file on the /boot partition. By referencing the config.txt video page you can set the output of the HDMI to a specific mode. In your case this may require custom settings which are described here within the raspberry pi forum.

You specify the new mode in config.txt with the following config string:

hdmi_cvt=<width> <height> <framerate> <aspect> <margins> <interlace> <rb>

Where:

Value       Default     Descriptionwidth       (required)  width in pixelsheight      (required)  height in pixelsframerate   (required)  framerate in Hzaspect      3           aspect ratio 1=4:3, 2=14:9, 3=16:9, 4=5:4, 5=16:10, 6=15:9margins     0           0=margins disabled, 1=margins enabledinterlace   0           0=progressive, 1=interlacedrb          0           0=normal, 1=reduced blanking