Forums

How to add image on tkinter gui window?

i am new in python, please guide me how to add image on tkinter gui window, see my code below, I want set image in background, my program is simple pulse counter Please guide me, my code

Blockquote

from tkinter import * import paho.mqtt.client as mqtt import time from tkinter import messagebox

def on_closing(): Window.destroy() quit()

DB_FILE = "db.txt" OLD_VAL = [line.rstrip('\n') for line in open(DB_FILE)] print(OLD_VAL)

y = int(OLD_VAL[0])

def on_connect(client, userdata, flags, rc): print ("Connected with result code" + str(rc)) client.subscribe("/esp/pot")

def on_message(client, userdata, msg): tag = msg.payload global count global x global y global E1 global E2 global E3 global E4 global E5 global E6 global E7 if (msg.payload.decode('ascii') == "N1") or (msg.payload.decode('ascii') == "N2") or (msg.payload == "N3"): a

E7.set(E5.get())
E5.set(E3.get())
E3.set(msg.payload.decode('ascii'))

E6.set(E4.get())
E4.set(E2.get())

with open(DB_FILE, "r") as file:
    lines = file.readlines()
    lines[1] = E3.get() + "\n"
    lines[2] = str(y) + "\n"
    lines[3] = E5.get() + "\n"
    lines[4] = E4.get() + "\n"
    lines[5] = E7.get() + "\n"
    lines[6] = E6.get() + "\n"

with open(DB_FILE, "w") as file:
    for line in lines:
        file.write(line)


if msg.payload.decode('ascii') == "N1":
    client = mqtt.Client()
    client.connect("localhost",1883,60)
    client.publish("/feeds/N1", (y));
    print ("N1")
    print (y)  
    E2.set(y)   
    client.disconnect();


if msg.payload.decode('ascii') == "N2":
    client = mqtt.Client()
    client.connect("localhost",1883,60)
    client.publish("/feeds/N2", (y));
    print ("N2")
    print (y)
    E2.set(y)
    Window.update_idletasks()
    client.disconnect();

if msg.payload == "N3":
    client = mqtt.Client()
    client.connect("localhost",1833,60)
    client.publish("/feeds/N3", (y));
    print (y)
    E1.set(y)
    Window.update_idletasks()
    client.disconnect();


y = 0        
E1.set(y)
Window.update_idletasks()

else: count = int(msg.payload) x = count y = (x+y) print (y)

with open(DB_FILE, "r") as file:
    lines = file.readlines()
lines[0] = str(y) + "\n"
with open(DB_FILE, "w") as file:
    for line in lines:
        file.write(line)

E1.set(y)
Window.update_idletasks()

Window = Tk() Window.geometry("1150x600")

E1 = StringVar()

E2 = StringVar() E3 = StringVar()

E4 = StringVar() E5 = StringVar()

E6 = StringVar() E7 = StringVar()

E1.set(y)

E2.set(OLD_VAL[2]) E3.set(OLD_VAL[1])

E4.set(OLD_VAL[4]) E5.set(OLD_VAL[3])

E6.set(OLD_VAL[6]) E7.set(OLD_VAL[5])

Total_PC_Label = Label(Window, fg="blue", text = "Total Pulse Count", font= ("Helvetica", 36)).grid(row=1, column=1) Total_PC_Entry = Entry(Window, fg="red", textvariable = E1, font= ("Helvetica", 36)).grid(row=2, column=1)#, ipady = 10, ipadx = 10, pady = 10, padx = 10)

Label(Window).grid(row=3, column=1) Label(Window).grid(row=3, column=2) Label(Window).grid(row=3, column=3) Label(Window).grid(row=4, column=1) Label(Window).grid(row=4, column=2) Label(Window).grid(row=4, column=3) Label(Window).grid(row=5, column=1) Label(Window).grid(row=5, column=2) Label(Window).grid(row=5, column=3) Label(Window).grid(row=6, column=1) Label(Window).grid(row=6, column=2) Label(Window).grid(row=6, column=3) Label(Window).grid(row=7, column=1) Label(Window).grid(row=7, column=2) Label(Window).grid(row=7, column=3) Label(Window).grid(row=8, column=1) Label(Window).grid(row=8, column=2) Label(Window).grid(row=8, column=3)

Label(Window).grid(row=9, column=1)

Label(Window).grid(row=9, column=2)

Label(Window).grid(row=9, column=3)

Label(Window).grid(row=10, column=1)

Label(Window).grid(row=10, column=2)

Label(Window).grid(row=10, column=3)

Total_PS_Label = Label(Window, fg="red", text = "Total Pulse Send1", font= ("Helvetica", 36)).grid(row=11, column=1) Total_PS_Entry = Entry(Window, fg="blue", textvariable = E2, font= ("Helvetica", 36)).grid(row=12, column=1, ipadx = 10, padx = 10)

Total_PS_Label = Label(Window, fg="red", text = "Total Pulse Send2", font= ("Helvetica", 36)).grid(row=13, column=1) Total_PS_Entry = Entry(Window, fg="blue", textvariable = E4, font= ("Helvetica", 36)).grid(row=14, column=1, ipadx = 10, padx = 10)

Total_PS_Label = Label(Window, fg="red", text = "Total Pulse Send3", font= ("Helvetica", 36)).grid(row=15, column=1) Total_PS_Entry = Entry(Window, fg="blue", textvariable = E6, font= ("Helvetica", 36)).grid(row=16, column=1, ipadx = 10, padx = 10)

No_Label = Label(Window, fg="red", text = "No.1", font=("Helvetica", 36)).grid(row=11, column =2) No_Entry = Entry(Window, fg="blue",textvariable = E3, font=("Helvetica", 36)).grid(row=12, column=2)#, ipady = 10, ipadx = 10, pady = 10, padx = 10)

No_Label = Label(Window, fg="red", text = "No.2", font=("Helvetica", 36)).grid(row=13, column =2) No_Entry = Entry(Window, fg="blue",textvariable = E5, font=("Helvetica", 36)).grid(row=14, column=2)#, ipady = 10, ipadx = 10, pady = 10, padx = 10)

No_Label = Label(Window, fg="red", text = "No.3", font=("Helvetica", 36)).grid(row=15, column =2) No_Entry = Entry(Window, fg="blue",textvariable = E7, font=("Helvetica", 36)).grid(row=16, column=2)#, ipady = 10, ipadx = 10, pady = 10, padx = 10)

client = mqtt.Client() client.connect("localhost",1883,60) client.on_connect = on_connect client.on_message = on_message Window.protocol("WM_DELETE_WINDOW", on_closing) Window.update_idletasks() Window.update() client.loop_forever() Window.mainloop()

Blockquote

Tell me where the add image code and which code i can use for that? can i take image from pi folder?

tkinter would not work on PythonAnywhere, which is a server (GUI-less) environment.