How do i turn off the button "glowing" on python tkinter (Linux)? - python

So, i've been learning a bit of tkinter and i've just found out that some things like buttons work differently from windows to linux... On windows everything works fine but on linux, when i over the button (with an image) it glows and, honestly, i can't figure out how to fix this.
from tkinter import *
import tkinter as tk
import PySimpleGUI as sg
root = tk.Tk()
root.title("Test")
root.geometry("500x500")
close_icon = PhotoImage(file='assets/Close Button.png')
close_button = tk.Button(root, image=close_icon, command=root.destroy, borderwidth=0)
close_button.pack()
if __name__ == "__main__":
root.mainloop()
What it should always look like:
What it looks like when i hover it:

Try to set option activebackground to be the same background color of the window.
color = root.cget("background")
close_button.configure(activebackground=color)

Related

Is there away to hide the text inside a button/label in Tkinter?

I'm a Python beginner that trying to learn my way through Tkinter, and I need your help.
Let say I create a simple button like this:
import tkinter as tk
window = tk.Tk()
button = tk.Button(text="Hello World!")
button.pack()
window.mainloop()
Is there a way that I can hide and then display the text again? Given that I can create two more buttons that will do the job of hiding and displaying. I have tried to use button.pack_forget(), but it will hide the entire button instead.
Any help would be appreciated.
To make it look like the buttons text vanishes,you can make the text color as same as the background color via cget method.
import tkinter as tk
def hide_text():
color = button['bg']
button.config(foreground=color, activeforeground=color)
window = tk.Tk()
button = tk.Button(text="Hello World!",command=hide_text)
button.pack()
window.mainloop()
Approach 2 ttk.Button:
import tkinter as tk
from tkinter import ttk
def hide_text():
button.config(text='')
window = tk.Tk()
button = ttk.Button(text="Hello World!",width=100,command=hide_text)
button.pack()
window.mainloop()
Approach3 using style:
import tkinter as tk
from tkinter import ttk
def change_button_style(event):
widget = event.widget
if widget['style'] == 'TButton':
widget.configure(style='VanishedText.TButton')
else:
event.widget.config(style='TButton')
BACKGROUND = '#f0f0f0'
FOREGROUND = '#000000'
window = tk.Tk()
window.configure(bg=BACKGROUND)
style = ttk.Style()
style.theme_use('default')
button = ttk.Button(text="Hello World!",style='VanishedText.TButton')
button.bind('<ButtonPress-1>',change_button_style)#,command=change_button_style
button.pack()
style.map('VanishedText.TButton',
foreground =[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)],
background =[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)],
focuscolor=[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)])
window.mainloop()
You Can Simply Config And Set Text :
import tkinter as tk
from tkinter import ttk
win=tk.Tk()
def hidetext():
button.config(text="")
button=ttk.Button(win,text="FooBar",command=hidetext)
button.pack()
win.mainloop()

How will I make a tk window which cannot be minimized in Python Tkinter?

I tried win.overrideredirect(True) it didn't work...
My Code goes like this:
from tkinter import *
win = Tk()
win.resizable(0,0)
win.wm_protocol("WM_SAVE_YOURSELF", lambda: print("On exit"))
Label(win, text="Tk Window").pack()
win.mainloop()
Specs:
Python 3.9.6 [Latest],
Pip 21.1.3,
OS: Windows 10 Home
I want to make the minimize button to be disabled...
Please help
The thing #N1CK145 posted didn't work for me but I am guessing that he/she tried to do this:
import Tkinter as tk
root = tk.Tk()
root.attributes("-toolwindow", True)
root.mainloop()
If you want to know more attribute options look here
try resizeable function like this :
win= Tk()
win.geometry("750x250")
win.resizable(False, False)
Found this here:
import Tkinter as tk
root= tk.Tk()
root.title("wm min/max")
# this removes the maximize button
root.resizable(0,0)
# # if on MS Windows, this might do the trick,
# # but I wouldn't know:
# root.attributes(toolwindow=1)
# # for no window manager decorations at all:
# root.overrideredirect(1)
# # useful for something like a splash screen
root.mainloop()

how to change an app icon in tkinter? I trayed to look online but it didn't work

I'm traying to make an app GUI using tkinter. how do I change the window icon?
I trayed to do:
import tkinter as tk
root = tk.Tk()
root.iconbitmap('app_icon.ico')
root.mainloop
I think you have to show that path to the ico picture, try this:
import tkinter as tk
root = tk.Tk()
root.iconbitmap('/path/to/ico/app_icon.ico')
root.mainloop()
Below code worked for me, you can check this for your scenario
from tkinter import *
root=Tk()
p1 = PhotoImage(file = "/path/to/ico/app_icon.ico")
root.iconphoto(False, p1)
root.mainloop()

Make a GUI that doesn't show in taskbar

I am trying to display a tkinter window (with an image,in my case) that doesn't show on the taskbar. I've already tried using the method described here, but the person that answered didn't provide an example so I am unable to replicate it (and I'm using Windows 10 so maybe that is another factor to consider).
My code is here. I am using Python 3.5
from tkinter import Toplevel, Tk, Label, PhotoImage
win = Tk()
win.attributes('-alpha', 0.0)
win.iconify()
window = Toplevel(win)
window.geometry("500x500+100+100")
window.overrideredirect(1)
photo = PhotoImage(file="testfile.png")
label = Label(window, image=photo)
label.pack()
win.mainloop()
The linked question contained an interesting comment recommending to reverse what was done in that other answer. Along with this accepted answer that explains that what is needed is just to set the WS_EX_TOOLWINDOW style, a simple example is:
import tkinter as tk
import tkinter.ttk as ttk
from ctypes import windll
GWL_EXSTYLE=-20
WS_EX_TOOLWINDOW=0x00000080
def set_toolwindow(root):
hwnd = windll.user32.GetParent(root.winfo_id())
style = windll.user32.GetWindowLongPtrW(hwnd, GWL_EXSTYLE)
style = style | WS_EX_TOOLWINDOW
res = windll.user32.SetWindowLongPtrW(hwnd, GWL_EXSTYLE, style)
# re-assert the new window style
root.wm_withdraw()
root.after(10, lambda: root.wm_deiconify())
def main():
root = tk.Tk()
root.wm_title("AppWindow Test")
button = ttk.Button(root, text='Exit', command=lambda: root.destroy())
button.place(x=10,y=10)
#root.overrideredirect(True)
root.after(10, lambda: set_toolwindow(root))
root.mainloop()
if __name__ == '__main__':
main()

No way to color the border of a Tkinter Button?

It works with some other widgets, but not with Buttons.
from Tkinter import *
root = Tk()
root.geometry("600x300+400+50")
btn_up = Button(root, text='Go UP')
btn_up.config(highlightbackground="red", highlightcolor="red", highlightthickness=10, relief=SOLID)
btn_up.pack()
root.mainloop()
Python 2.7 - Windows 10
I am using linux and when I run your code, I get a button with a thick red border, so it looks like that the default Windows theme does not support highlightthickness while the default linux theme does.
If you want to change the border color, it is possible with some ttk themes like 'clam':
from Tkinter import *
import ttk
root = Tk()
style = ttk.Style(root)
style.theme_use('clam')
style.configure('my.TButton', bordercolor="red")
ttk_button = ttk.Button(root, text='Go UP', style='my.TButton')
ttk_button.pack()
root.mainloop()
However, changing the borderwidth, with style.configure('my.TButton', borderwidth=10) does not increase the width of the red border as expected.

Categories

Resources