I'm writing a program and I need to inform the user about some changes with a popup message, but not a popup window. Something like the rectangle informing about new message in Kadu - no window, just a bitmap drawn directly on the screen for a few seconds.
I wonder if there is a simple way to do that with win32 package or Tkinter, and handle the event when the user clicks on the rectangle.
Actually the message would be constant, so the bitmap might be loaded from a file, but I still don't know how to start.
Any ideas, please?
Regards, mopsiok
I am using wxPython and looking for a way to have a popup message. Now I am using a popupmenu in which I append each item of the menu with one line of the message.
Actually I found the answer to my question. That's my code using Tkinter, hope it will help you.
from Tkinter import Tk, Label
from Image import open as iopen #doesn't needed if you won't display image
from ImageTk import PhotoImage #as before
root = Tk()
img = PhotoImage(iopen("some_path")) #load an image
label = Label(root, image=img)
label.image = img
label.bind("<Button-1>", Click)
label.pack()
root.geometry('-0-40') #place in the right-bottom corner
root.wm_attributes("-topmost", 1) #popup
root.overrideredirect(1)
root.mainloop()
def Click(event): #close the window if image clicked
root.destroy()
print 'window closed'
Related
When creating a button with an image on it you can specify its background color to be the same as the root background color with the bg='color' parameter so that if you have an image with transparent background the result seems nice.
However for some reason when you click on the button, there is a white flash that covers the button for as long the click happens.
I have this code:
from tkinter import Tk, Button
from PIL import Image, ImageTk
root = Tk()
root.config(bg='black')
image = Image.open('your_image_file').resize((50, 50))
image = ImageTk.PhotoImage(image)
button = Button(root, width=50, height=50, image=image, bg='black')
button.pack()
root.mainloop()
After importing PIL (if not already) and then filling the appropriate path name to your image the code should run without any problems
Notice that when you click the image a whity flash that covers the button appears?
I want to get rid of that
I've tried passing the parameters highlightcolor highlightbackground and highlightthickness and playing around with them but nothing really changes whether I for example change highlightthickness=0 or highlightbackground='blue'
Is there any way to do that properly?
Thanks in advance!
Oh, I just now realised the activebackground parameter does the job
Everything okay now I can modify the whity flash color :P
I have a tkinter window that I have given a background picture by creating a Label widget with a PhotoImage instance (referencing the image instance through Label attributing).
However when I run the script and move the main window below the start menu (am using Windows 10) or past the sides of the screens for even one moment, all the widgets packed onto the Label (w/ background pic) completely disappear.
They only come back (somewhat) upon hovering over them with the mouse it seems. Also the background picture remains and continues to fill the screen. Could it be that the background picture Label is being "lifted" and makes it seem like the widgets are disappearing? If so, how can I prevent this from happening?
The fix that I have found for now is to not use a Label with a PhotoImage as the parent "frame", but instead use a typical Frame widget with only a background color, but this is not ideal.
import tkinter as tk
root = tk.Tk()
root.geometry('600x350+600+300')
root.resizable(width=False, height=False)
boxBg = '#666'
frameBg = '#fff'
#problem method
backgroundImg = tk.PhotoImage(file='program_media/background.png')
bgFrame = tk.Label(root, image=backgroundImg)
bgFrame.image = backgroundImg
#less than ideal solution so far
#bgFrame = tk.Frame(root, bg='#fff')
bgFrame.pack(expand=1, fill=tk.BOTH)
mainFrame = tk.Frame(bgFrame)
mainFrame.pack(side=tk.TOP)
title = tk.Label(mainFrame, text='Test String')
title.pack(side=tk.TOP)
#widget creation code packed within mainFrame
#...
#... All these widgets (including mainFrame above) are disappearing
#...
#end of widget creation code
root.mainloop()
See what I mean in this screenshot of BEFORE and AFTER moving the main window below the start menu.
I am having a problem with getting tkSimpleDialog to take focus over my fullscreen window GUI. I have a GUI that I am trying to use a dialog window to use as an admin password to close the GUI (like a kiosk mode app) using root.quit(). The problems with this are that the dialog does not come in front of the parent windows if the parent windows are fullscreen. Additionally, I would like to make the tkSimpleDialog go fullscreen as well.
Here is the code for how the dialog box is being called/created using the tkSimpleDialog.py in my program:
def check_admin_password():
# use askstring here to verify password.
pass_attempt = simpledialog.askstring("Verifying access","Please enter Admin password", parent=window, show="*")
if pass_attempt == "password":
root.quit() # used whatever your instance of Tk() is here in place of root.
admin_minimize_button = Button(window, text = "Administrator", command = check_admin_password, width=35, height=12)
admin_minimize_button.grid(row=4, column=0)
I am using the following code for the parent window and I believe there is something with the overrideredirect(True) that is affecting the focus of my dialog window:
qwindow = Toplevel(root) #renames Toplevel "popup" window frame to variable "qwindow"
qwindow.title('Mission Queue') #creates title on popup window
w, h = qwindow.winfo_screenwidth(), qwindow.winfo_screenheight() #aquires dimensions from display size
qwindow.geometry("%dx%d+0+0" % (w, h)) #sets window size to aquired dimensions
qwindow.overrideredirect(True) #removes top bar and exit button from parent window frame
I have tried editing the tkSimpleDialog.py file and adding an overrideredirect(True) line however that is not working. If more information is needed, let me know. Any advice would be much appreciated. Thanks in advance.
Due to my inability to figure out a solution using tkSimpleDialog, I attempted a different approach creating my own "dialog". Unfortunately, I was still unable to get the dialog to have the entry line take focus with the screen window being set to wm_attributes('-fullscreen', 'True') and overrideredirect(True). While it would have been better to have both work; for my purposes, it will work fine to just be fullscreen in my application without overrideredirect(True). For anyone with a similar issue or wanting to see more documentation on my progress, heres the link to my other forum post: (Tkinter Entry Widget with Overrideredirect and Fullscreen).
Thanks for the help!
Can tkinter create custom buttons from an image or icon like this?
It's possible!
If you check out the button documentation, you can use an image to display on the button.
For example:
from tkinter import *
root = Tk()
button = Button(root, text="Click me!")
img = PhotoImage(file="C:/path to image/example.gif") # make sure to add "/" not "\"
button.config(image=img)
button.pack() # Displaying the button
root.mainloop()
This is a simplified example for adding an image to a button widget, you can make many more cool things with the button widget.
I created a library called CustomTkinter, and with it you can create more or less exactly what is shown in the images above. CustomTkinter provides new widgets for Tkinter, which can be customised in color and shape. Here I tried to create something similar to the image above:
You can find the example code to the above image here.
There is not also a Button, but many other elements, and it also supports a dark and light theme:
You can check out the library here:
https://github.com/TomSchimansky/CustomTkinter
A simple example would be:
import tkinter
import customtkinter
customtkinter.set_appearance_mode("System")
customtkinter.set_default_color_theme("blue")
root_tk = customtkinter.CTk() # create CTk window like the Tk window
root_tk.geometry("400x240")
def button_function():
print("button pressed")
# Use CTkButton instead of tkinter Button
button = customtkinter.CTkButton(master=root_tk, command=button_function)
button.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
root_tk.mainloop()
which gives the following on macOS:
I used the code below (with different variable names for each section) to create a background image for each tkinter window. Each of these is initiated in a function and both work fine independently.
When loading one function from another however, the second fails to display an image. (I have tried importing all relevant in each function aswell). It works in the case that use tk.destruct(), however if If I want to keep it open, or hide it with . withdraw(), the image fails to display, rendering the second window useless.
background_image=tk.PhotoImage(...)
background_label = tk.Label(parent, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
Ok I've made up a solution for you. Basically all you need is to use tk.Toplevel() for the second tkinter window and make sure that the 'parent' is root2 so the image will appear in the second window.
I have used buttons for the images, you had labels so you may wish to change this, but buttons gave me a way to open a new tk window easily, I have also used .pack(), not .place(), as it was faster for me. May also be helpful for you to know that I used python 3.3 with windows so you might need a capital T for tkinter.
import tkinter as tk
root1 = tk.Tk()
def new_window():
root2 = tk.Toplevel()
# click the last button and all tk windows close
def shutdown():
root1.destroy()
root2.destroy()
background_image2 = tk.PhotoImage(file = '...')
background_button2 = tk.Button(root2, image = background_image2, command = shutdown)
background_button2.pack()
root2.mainloop()
background_image1 = tk.PhotoImage(file = '...')
# have used a button not a label for me to make another tk window
background_button1 = tk.Button(root1, image = background_image1, command = new_window)
background_button1.pack()
root1.mainloop()
#user2589273 Next time you should add more code so answers can be easily given, and tailored to you, just a suggestion. Hope this helps.