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.
Related
I am teaching myself python, but am running into problems with Tkinter. I created a simple program based on some videos I've been using to learn as I go. From all my research thus far, I cannot find a problem in the code, it is only four lines show below. The code creates a window called "tk" that is completely black with no text inside that is the correct size for what the text should take up if it were to be there.
I have tried to select the text inside the window to see if there is anything there, but there isn't. I've tried to change the background color to white using the bg command, but nothing changes with the window. I have opened "empty" windows using only the first and last line, which again creates a window of the correct size from everything I've seen online, but again is completely blank and black instead of white or gray as it should be.
I am using PyCharm (PyCharm 2021.3.1 (Community Edition)) as my compiler on a MacBook Pro. (iOS is on 12.0.1). Tkinter is on (Tcl 8.5 & Tk 8.5 (8.5.9)).
When I click run, everything seems to run smoothly with no errors. Honestly, I'm at a complete loss as to what the problem is. Any ideas on how to fix this?
from tkinter import *
root = Tk()
myLabel = Label(root, text="Hello World! Why can't you see me?")
myLabel.grid(row=0, column=0)
# root.configure(bg='white')
root.mainloop()
I had a similar issue to this due to running python 2.7.
When we updated to python 3.10 the issue disappeared.
Try checking your version of python.
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.
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.
I have searched wide and far for this question, but noone seems to know. I create a simple tkinter window (tcl 8.5) in python 2.7 and want it maximized, just as if I would hit the maximize button in top right corner. Using the -fullscreen option is not an option since it removes the title bar.
I tried the following:
import Tkinter
root = Tkinter.Tk()
root.overrideredirect(True)
# Set window to be size of screen
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
The problem is that the window is now below the Windows taskbar and some of my elements are therefore not shown. An easy hack would be to set height to screenheight-some_constant, or calculate some_constant based on data from the operating system. However, this seems like an extremely ugly hack.
Is there any way to maximize a window in tkinter in a clean way where the window is above the (Windows) taskbar and still has a title bar?
i know this is an old question but i have tested the following code on an XP system (32bit) with python 2.7.6, using TCL version 8.5 and tk version 8.5.
code:
import Tkinter as tk
root = tk.Tk()
root.state("zoomed")
root.mainloop()
this does start the window maximised on the primary monitor.
it does have pitfalls - i have been unable to make it maximise on another monitor, but does work reliably without covering the title bar or taskbar.
and as you want to keep the title bar i would leave out the overrideredirect.
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?