Beginner / its my 1st time asking a question over here (.. over anywhere else also) so let me apologies in advance if any rules would'have not been followed


I'am doing an interface on raspbian/tkinter to work with Raspberry Pi touchscreen. (simple commands done when touching differents parts of the screen)

Question is, I have multiple windows with buttons inside, how to insert image in some of them buttons ?

Position of the windows are determinant for the application, so use of top level or all buttons in single window etc is no good option imo...and I dont understand class enough to use this tool< any help on this would be much appreciated

It is probably not ideal but I created 3 different window with 2 buttons in each (see picture) . using a single window to hold all buttons is not good as it is limited to the N/S/E/W positioning.

when you lunch the software you got this

for exemple I try to put image instead of text buttons for the button on the bottom right (he blank one) it shows error :

Traceback (most recent call last):
  File "/home/pi/test/StackExchangeQ/test fullscreen button.py", line 50, in <module>
    b = Button(master, image=image, command=zone_x, height=16, width=10)
  File "/usr/lib/python3.5/tkinter/__init__.py", line 2221, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "/usr/lib/python3.5/tkinter/__init__.py", line 2151, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist

with the code below ;

import tkinter as tk
from tkinter import *
import PIL
from PIL import ImageTk

def zone_a() :
    print("")

def zone_b() :
    print("")

def zone_c() :
    print("")

def zone_d() :
    print("")

def zone_x() :
    print("")

#creation of 3 window
win1 = Tk()
win2 = Tk()
master = Tk()


# determine geometry and position of windows: win.geomtry(height,weight,x,y)
# x and y position from top left corner of the screen

win1.geometry("380x480+0+0")
win2.geometry("380x480+380+0")
master.geometry("50x480+760+0")

#suppress windows toolbars
win1.overrideredirect(True)
win2.overrideredirect(True)
master.overrideredirect(True)

Button(win1, text="ZoneA", command=zone_a, height=16, width=45).pack()
Button(win1, text="ZoneB", command=zone_b, height=16, width=45).pack()
Button(win2, text="ZoneC", command=zone_c, height=16, width=45).pack()
Button(win2, text="ZoneD", command=zone_d, height=16, width=45).pack()

#Thoses 2 buttons that I would like to change into image buttons.
#Button(win3, text="Exit", command=quit, height=16, width=10).pack()
#Button(win3, text="", command=zone_x, height=16, width=10 ).pack()
image = ImageTk.PhotoImage(file="button_zone-x.png")

Button(win2, text="Exit", command=quit, height=16, width=10).pack()
b = Button(master, image=image, command=zone_x, height=16, width=10)
b.image = image
b.pack()


mainloop() 

how can I get to the result I want with the positioning and knowledge constraints expressed before ?

  • This is a programming question which should be asked on StackOverflow. – Dmitry Grigoryev Sep 25 at 13:09

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.