Python button image border - python

I am trying to remove the border that is coming with the buttons in my program.
I've tried adding bd=0 and highlightthickness=0 to the Button() but it's just not working.
Can anyone suggest how to do this?
Current code example:
self.highscore_button_image = ImageTk.PhotoImage(file="images/buttons/highscore.png")
highscore_button = Button(self.menu_window, bd=0, highlightthickness=0, image=self.highscore_button_image)
highscore_button.place(x=266, y=273)
Buttons: I want to remove the white border... It also comes with a regular button without an image
Python version: 2.7.1
OS: Mac OSX Lion
Using Tkinter at the moment...

I'm not sure you can get rid of that border for a button. You could use a Label widget instead of a Button widget, and bind mouse events to it if it needs to be clickable. The Label widget doesn't have that button-y outline.

highscore_button = Button(....,relief=FLAT)
The relief style of a widget refers to certain simulated 3-D effects around the outside of the widget, it can be RAISED, SUNKEN,FLAT,GROOVE,RIDGE.

Removing border on buttons on Mac is currently not allowed. OS X is quite strict at this, there is no way to remove the border if using tkinter. I recently just had the same problem and all the methods I've tried are not working properly on Mac while most of them work on Windows.
However, it is possible to do it on Windows using "relief=FLAT" or "highlightthickness=0". You can also use "padx=0, pady=0" to have a really thin border, it will still exist.
You can also check out this site which talks about styling tkinter widgets(not only buttons but all the widgets): http://effbot.org/tkinterbook/tkinter-widget-styling.htm
You may change the colour of the border(ButtonFace) to the same colour of your background according to this site.
There is a similar question on button styling here as well: How to change the foreground or background colour of a Tkinter Button on Mac OS X?

Related

how to change a window border color in tkinter

I'm having a problem with changing the background of this part of the window:
How do I change it?
As I see you are using windows.
This color is set by the theme you are currently using. It is the same for every window.
So I cross out the possibility of only using the Tkinter module for this.
Tkinter is responsible for what is in the window but the window manager decides about the border. For example in Ubuntu the window would look totally different.
I guess, you would need some windows specific calls for that.
You can remove the border with root.overrideredirect(1) if I remember correctly.
PS: put "windows" into the tags of this question.

Changing Font Color of wxpython Radio Button

I am using wxpython version 2.9.4.0 and python 2.7.9.
I am trying the change the color of the text for a radio button.
I initialized by:
button = wx.RadioButton(panel, -1, 'Line', (200, 300))
I was able to change the color around the radio button by:
button.SetBackgroundColour((150, 150, 150))
But this is not the behavior I want. I want to change the color of the text, not the area around it. I expected that changing the foreground colour would change the text color of the radio button, as that is how the color is changed for static text (as shown here Change the colour of a StaticText, wxPython). The code I used for this is:
button.SetForegroundColour((0, 255, 0))
However, for reasons unknown to me, this did not change anything about the radio button.
Am I mistaken that this command should change the text color of the radio button, and if so, what is the proper command?
Thanks in advance!
The SetForegroundColoour and SetBackgroundColour methods are not guaranteed to work. The reason is that wxPython uses the native widgets for the OS it is running on. If the native widget does not support changing the text's color, then these methods will have no effect. Some widgets allow changes to color on Mac while the same widgets on Windows do not.
If you really need to change text color in a radio button, then you'll probably need to create a custom widget. See the following:
http://wiki.wxpython.org/CreatingCustomControls
Use a wx.RadioButton with no label
&
put a wxStaticText next to it
and set your foreground colour as u wish

Python Tkinter - Prevent Focus State on Lastly Clicked Button

I'm working on a basic PIN interface for a touch screen with Python 2.7 Tkinter and ttk. I'm developing the script on Windows but it will eventually be loaded on a Linux OS.
I am trying to prevent what is shown on the "6" button of the picture bellow, i.e. a dashed border around the button lastly clicked. Since I don't want people to easily steal the PIN from my users, I have to prevent this from happening, otherwise it becomes really easy to find out what their PIN are just by looking at the screen. I have noticed that this behavior becomes even more obvious on LINUX with something like a thick white border around the button.
I am calling my buttons inside a loop like this:
ttk.Style().configure('TButton', padding=11, relief="flat", background="#ccc", foreground="#393939", width=4,font='Arial 9')
btn = ttk.Button(window, text = txt, command = lambda txt=txt:self.addChar(txt))
btn.grid(row=row, column=col, padx=1, pady=1)
The solution is pretty simple: modify your addChar function to move the focus back to some other widget after it inserts the character.

TKinter leaving borders around widgets

When I put a button in on a colored background TKinter leaves this weird white box around the widget. For example the code below:
from Tkinter import *
root = Tk()
root.geometry("300x100+300+300")
root.configure(bg="red")
button = Button(root, text="Connect", highlightthickness=0)
button.pack()
root.mainloop()
What can I do to get rid of the white spacing?
The extra border is caused by the highlightthickness attribute. The default value is 1 (one); set it to zero to remove the border. This border shows when the button has keyboard focus.
However, it appears you're running this on OSX. OSX buttons are a bit less configurable than on other platforms. Setting highlightthickness to zero won't help. The best you can do is set highlightbackground to the same color as your background so that it blends in.
This problem has been plaguing Macs for years. But as of Python 3.7 it's safe-ish to install from Python.org instead of Homebrew. This problem disappears when Python is installed from Python.org instead of running the Homebrew version.

Adding a tooltip to an image

I'm trying to add a tooltip to an image. Following a PyGTK tutorial it seems that this should work:
image = gtk.Image()
image.set_from_file(image_path)
box.pack_start(image, expand=False)
tooltip = gtk.Tooltips()
tooltip.set_tip(image, "Hello!")
Except it doesn't. Nothing happens when I mouse over the image. However, I know that works with buttons (I ran the sample code from the tutorial).
With GTK 2.12 and above, I could probably just use image.set_tooltip_text("Hello!") but I'm stuck at 2.10.4 and have to use gtk.Tooltips.
Edit
According to the documentation for gtk.Tooltips:
Tooltips can only be set on widgets which have their own X window. To check if a widget has its own window use widget.flags()&gtk.NO_WINDOW. To add a tooltip to a widget that doesn't have its own window, place the widget inside a gtk.EventBox and add a tooltip to the eventbox instead.
So that solves my problem but leaves me a bit confused. I checked the flags for a button and it has the same gtk.NO_WINDOW flag that images have. So why don't buttons need an EventBox but images do?
To satisfy its interface, GktButton creates an event box (well, something like an event box) for itself, internally. I.e. it captures events in a non-visible gdk window. GtkImage doesn't have a similar interface to satisfy so it doesn't need to capture events.
Perhaps it's an accident of the button's internal implementation that using the tooltip interface works without embedding a button in an EventBox or perhaps the tooltip interface actually depends upon a gdk window whether it's visible or not and the Widget interface lacks that sort of flag.

Categories