I simply just want to have a button with an image (say an apple) and place it on my window without the background of the button.
Here is some sample code I've rushed:
from tkinter import *
root = Tk()
testbutton = Button(root, text="If this was an image I should be able to click the image only").pack()
root.mainloop()
This makes a button but it has a grey background that a. conflicts with my window background and b. allows users to click the image background.
I'm okay with the solution being windows specific however if there are mac and linux solutions as well i want those too.
I also dont want to use root.overridedirect(True) either.
Thanks in advance!
Related
I am making a GUI using tkinter, I have some Button icons on my screen, that when user clicks, they execute their specific function. for example this one:
def screenshot():
# root.iconify()
myScreentshot=pyautogui.screenshot()
file_path=filedialog.asksaveasfilename(defaultextension='.png')
myScreentshot.save(file_path)
screenshot_image = tk.PhotoImage(file='images/app6.png')
screenshot = tk.Button(root,image=screenshot_image,bg='#0000CD',command=screenshot)
screenshot.place(x=640,y=500)
the problem is icons image. when I use the image as a button icon on my page, they always have a square or a rectangle on their background, although I use bg to set its color like my main page but I can still see it has a background.
How can I simply display the shape of the image without the background of the shape?
The square or rectangle is called the "relief". It is one of the visual cues that this is a button that can be pressed.
If you don't want that, set it to "flat":
screenshot = tk.Button(
root,
image=screenshot_image,
relief="flat",
bg='#0000CD',
command=screenshot
)
I would however advise you not to do this. The relief is there so that buttons are recognizable as such. Removing that in some places makes for an inconsistant user interface which can confuse new users.
I want to make a borderless widget with tkinter that lets the user decide if they want the application to show on top of other programs or not.
I'm aware that in order to remove the borders and the toolbar I can use the method root.overrideredirect(True), but this also makes the window topmost automatically, as it makes the window manager ignore the widget.
Because of this, the root.attributes("-topmost",False) and root.wm_attributes("-topmost", False) command don't work.
Is there a way to achieve what I need? For example, another method to hide the window manager decorations or some other way to hide/restore the widget when it loses the focus?
By the way, I'm using Linux Mint 20.2 Cinnamon v5.0.5 with Python 3.8.10.
from tkinter import *
root = Tk()
root.overrideredirect(True)
root.resizable(False, False)
root.geometry("420x100")
root.attributes("-topmost", False)
#root.wm_attributes("-topmost", False)
exitbtn = Button(root, text='Exit',command=root.destroy)
exitbtn.pack(side=BOTTOM)
root.mainloop()
EDIT:
After some search, I've found that root.wm_attributes('-type','splash') works exactly as I want. But it seems that this will only work on Linux. How could I achieve that on Windows?
I had the same question and this worked for me:
Tkinter, Windows: How to view window in windows task bar which has no title bar?
PS: I'm new here so I cannot comment.
Edit: It's for Windows I think. I haven't tried on Linux yet.
When using overrideredirect or something like root.wm_attributes('-type', 'splash') my entry box cannot get focus on user's click.
I am developing a GUI which I want to maintain the style of previous windows and not have this window stand out with the window header. How do I get rid of the RPI window bar but maintain entry box functionality.
When using the splash window type (Linux only), you can make the entry get the keyboard focus by using focus_force(), e.g. binding it to the left click.
import tkinter as tk
root = tk.Tk()
root.wm_attributes('-type', 'splash')
e = tk.Entry(root)
e.pack()
# force focus on left click:
root.bind('<1>', lambda ev: ev.widget.focus_force())
root.mainloop()
I need to make an application, in which I need to place a tkinter button widget-'b1', inside another tkinter button widget-'b2'.
b2,however disappears and appears only after mouse is hovered over it.
Made a simple code without using OOP. Firstly made a master root window,added button 'b1'. Then using 'b1' as a master, added button 'b2'. The callbacks of both buttons are different. The problem is,button 1(in light blue color) appears normally visible. However second button(in red color) doesnt gets displayed. It appears only when you hover the mouse over it.
import tkinter as tk
root=tk.Tk()
b1=tk.Button(root,height=7,width=10,bg='light blue',command=print('B1'))
b1.place(x=0,y=0)
b2=tk.Button(b1,height=3,width=5,bg='red',command=print('B2'))
b2.place(x=2,y=2)
root.update()
Is it possible that both buttons are displayed simultaneously?.
Help is appreciated. Thanks in advance
This version of code is just for simplification. Later will implement more buttons and OOP as well. Sorry for mistakes if any.
I currently have a python script which launches a tkinter GUI instance, with a button that once clicked takes a screenshot.
When I run this script under python.exe, the tkinter resolution is fine and the screenshot captures the whole screen. However, when using pythonw.exe, the tkinter window resolution changes (button gets bigger for example) and the screenshot only captures a portion of the screen - the top left hand corner normally.
I need to use pythonw.exe in order to prevent the console window appearing.
Does anyone know why the tkinter window resolution and the screenshot capture is being effected? Presumably the effect on the resolution is why the screenshot capture is being reduced as well.
I am fairly new to Python, so any help with this would be greatly appreciated, below is snippet of the code for the tkinter window and the screenshot functionality. To reiterate this functionality runs completely fine under python.exe.
The screenshot functionality using ImageGrab:
callback1():
ImageGrab.grab_to_file('test.png')
The tkinter window:
master = Tk()
master.wm_attributes("-topmost", 1)
master.title("Report")
master.configure(background='white')
master.iconbitmap(default='icon.ico')
master.resizable(0, 0)
frame1 = Frame(master, bg='white')
frame1.pack(side=BOTTOM, fill=X)
button1 = Button(frame1, compound=BOTTOM, width=307, height=82,
image=photo1, bg='white', command=callback1)
button1.pack(side=TOP, padx=2, pady=8)
I have now fixed this. It seems it was related to the compatibility settings for pythonw.exe in Windows. Changing the following fixes the image capture, but also the tkinter window resolution:
Go to your python directory (c:/python27/ for me)
Right click python.exe and select properties
Select the compatibility tab
Press the "Change settings for all users" button
Check the "Disable display scaling on high DPI settings" box
Credited by this post:
Python Imaging Library fails to grab whole screen
Hopefully this helps someone with the same issues. It does beg the question as to how this can be done automatically, as for users of a python application it is not user-friendly for them to have to change these settings.