![]() import tkinter as tk from tkinter import ttkfrom PIL import Image,ImageTkmy_w = tk.Tk()my_w.geometry("840x570") #my_img = tk.PhotoImage(file = "D:\\top2.png") my_img2 = ImageTk.PhotoImage(Image.open("D:\\my_data\\background1.jpg"))bg = tk.Label(my_w, image=my_img2)bg.place(x=0, y=0, relwidth=1, relheight=1)my_w.mainloop() To use JPG image we have to include PIL library. import tkinter as tkmy_w=tk.Tk()from PIL import Image,ImageTkmy_w.geometry('300x100')my_w.title('my_img = ImageTk.PhotoImage(Image.open("D:/images/top2.jpg"))b1=tk.Button(my_w,image=my_img)b1.grid(row=1,column=1)my_w.mainloop() Background Image of the windowĬhange the path of the image used at my_img2. %pip freezeHere is the code to display Jpg image over a button. You can check the status by using this command and check all installed libraries. from PIL import ImageTk, Image If PIL ( Python Image Library ) is not installed then use PIP to install it. Import tkinter as tkmy_w=tk.Tk()my_w.geometry('300x100')my_w.title('= tk.PhotoImage(file = "D:/images/top2.png") b1=tk.Button(my_w,image=my_img)b1.grid(row=1,column=1)my_w.mainloop() Read more on how to disply MySQL Blob binary data (image) using button PIL ( Python Imaging Library )To install use this command at command prompt pip install pillow Python Imaging Library : Pillow →įor. Import tkinter as tk my_w = tk.Tk()my_w.geometry("400x200") l1 = tk.Label(my_w, width=15 )l1.grid(row=1,column=1) my_img = tk.PhotoImage(file = "D:\\top2.png") l2 = tk.Label(my_w, image=my_img )l2.grid(row=1,column=2) my_w.mainloop()You may change you path based on the location of the image file in your system. Import tkinter as tkmy_w=tk.Tk()my_w.geometry('300x100')my_w.title('my_w.iconbitmap('D:\\images\\favicon.ico')my_w.mainloop() Adding image using Label Here \f can be understood as form feed, so we used two backslashes. When we close the window it will still appear in the Taskbar as a System Tray icon.Tkinter displaying icon or JPG PNG image in windows by using Label or button using PILLOW libraryĮscape the path by using two backslashes if you have any char with can be used with backslash. If you will run the above code, it will display a window. Win.protocol('WM_DELETE_WINDOW', hide_window) Icon=pystray.Icon("name", image, "My System Tray Icon", menu) Menu=(item('Quit', quit_window), item('Show', show_window)) # Hide the window and show on the system taskbar # Define a function to show the window again # Create an instance of tkinter frame or window Import the required libraries − Pystray, Python PIL or Pillow.ĭefine a function hide_window() to withdraw the window and provide the icon in the system tray.Īdd and define two menu items, " Show" and " Quit".Īdd a command in the menu items by defining a function for Show and Quit. To create a System Tray icon, you can follow these steps, To install pystray in your machine you can type " pip install pystray" command in your shell or command prompt. It has many inbuilt functions and methods that can be used to configure the system tray icon of the application. To create a System Tray icon of a tkinter application, we can use pystray module in Python. The system tray icon is visible in the taskbar. ![]() ![]() It typically shows which application is currently running. A System Tray icon is used for showing the application’s running state in the taskbar.
0 Comments
Leave a Reply. |