This question already has an answer here:
How to get rid of widget border?
(1 answer)
Closed 5 years ago.
When using a fullscreen canvas, such as in the following code:
from tkinter import *
root = Tk()
root.attributes("-fullscreen", True)
canvas = Canvas(root, background = "red") # I use a red background for visibility.
canvas.pack(fill = BOTH, expand = True)
I get a small (2 pixel) border around my entire screen. This happens on any computer I test the code on.
Is there a way I can remove this border, as in have the canvas fill in the entire screen up to the very edge?
Image of my full screen, with the fullscreen canvas
Thanks
Set this option to zero:
canvas = Canvas(root, background="red", highlightthickness=0)
Check the documentation to find the full list of options. It can help you to resolve many similar problems.
Related
I want to create a frameless resizable window, the problem is, when there are many widgets, they glitch when the window is resized using a Sizegrip. The only solution that I found that removes this glitch effect is to update the window during the resize.
Unfortunately if you keep resizing for a few seconds a recursion error will occur and I have no idea why.
Here's my code
import tkinter as tk
from tkinter.ttk import Sizegrip
root = tk.Tk()
root.overrideredirect(True)
root.geometry("500x400")
def on_resize(event):
global root
root.update()
tk.Label(root, text = "Use the bottom right grip to resize, try for a few seconds").pack()
sg = Sizegrip(root)
sg.pack(side = tk.BOTTOM, anchor = tk.E)
sg.bind("<B1-Motion>", on_resize)
root.mainloop()
Check if you're already resizing and don't call root.update() again.
resizing = False
def on_resize(event):
global resizing
if not resizing:
resizing = True
root.update()
resizing = False
This question already has answers here:
How to make a Button using the tkinter Canvas widget?
(2 answers)
Closed 2 years ago.
I want to make a button using the Tkinter canvas. But I can't find anything on how to do it. I've tried using the regular button widget, but those don't display nor do anything. If anyone has a way of doing, please tell me!
This works.
canvas.create_rectangle(x1, y1, x1+w, y1+h, fill=fill, tags=tag, width=width)
canvas.tag_bind(tag, "<Button-1>", function)
2nd line is key :)
This generates a button on a canvas, the canvas setting should be changeable without changing the button. Found here https://python-tricks.com/button-in-tkinter/
import tkinter as tk
screen = tk.Tk()
#Screen size
screen.geometry('300x300')
#Button
button_tk = tk.Button(screen, text="Hello")
button_tk.pack(side='top')
screen.mainloop()
I am making a chess program and I want to be able to drag the pieces. In order to do this, I put the image of the piece on a Canvas so it can be dragged (I can also use a Label if I want). However, when I drag the piece there is a white square that surrounds the image of the piece.
When I researched the problem, many people gave this solution:
drag_canvas = Canvas(self, height=80, width=80, bg="yellow")
root.wm_attributes("-transparentcolor", "yellow")
This caused the background to be transparent but it was not the chessboard that was visible, it was the program behind the GUI
.
Is there any way I can have the background be transparent and show the chessboard behind rather than the program behind the tkinter window?
Note: I do not mind using any other widget (e.g. a Label) but they must use modules that come default with Python (so no PIL) as this program needs to be used in an environment where I cannot download other modules.
Question: How to make a tkinter canvas background transparent?
The only possible config(... option, to set the background to nothing
c.config(bg='')
results with: _tkinter.TclError: unknown color name ""
To get this result:
you have to hold the chess board and figures within the same .Canvas(....
self.canvas = Canvas(self, width=500, height=200, bd=0, highlightthickness=0)
self.canvas.create_rectangle(245,50,345,150, fill='white')
self.image = tk.PhotoImage(file='chess.png')
self.image_id = self.canvas.create_image(50,50, image=self.image)
self.canvas.move(self.image_id, 245, 100)
Tested with Python: 3.5 - TkVersion: 8.6
A windows only solution is to use the pywin32 module that can be installed with:
pip install pywin32
With pywin32 you can alter the window exstyle and set the canvas to a layered window. A layered window can have a transparent colorkey and is done in the example below:
import tkinter as tk
import win32gui
import win32con
import win32api
root = tk.Tk()
root.configure(bg='yellow')
canvas = tk.Canvas(root,bg='#000000')#full black
hwnd = canvas.winfo_id()
colorkey = win32api.RGB(0,0,0) #full black in COLORREF structure
wnd_exstyle = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
new_exstyle = wnd_exstyle | win32con.WS_EX_LAYERED
win32gui.SetWindowLong(hwnd,win32con.GWL_EXSTYLE,new_exstyle)
win32gui.SetLayeredWindowAttributes(hwnd,colorkey,255,win32con.LWA_COLORKEY)
canvas.create_rectangle(50,50,100,100,fill='blue')
canvas.pack()
Explaination:
First we need the handle of the window which is called hwnd and we can get it in tkinter by .winfo_id().
Next we get the actual extended window style by GetWindowLong and ask specific for extended style information with win32con.GWL_EXSTYLE.
After that we do a bitwise operation in hexadezimal to alter the style with wnd_exstyle | win32con.WS_EX_LAYERED the result is our new_style.
Now we can set the extended style to the window with SetWindowLong. Finally we have our LayeredWindow which has additional Attributes we can work with. A transparent ColorKey can be set with SetLayeredWindowAttributes while we just use LWA_COLORKEY the alpha parameter has no use to us.
Important note: After defining a transparent colorkey, everything in that canvas with that color will be transparent.
This question already has an answer here:
tkinter color of disabled buttons / disabled optionmenus
(1 answer)
Closed 8 years ago.
In my Program I use a picture in a button. If I now disable this button with button.configure(state="disabled"), I get a white overlay over the whole button. Can I remove this overlay? If yes how? Thanks in advance. Here is an example code:
import Tkinter as tk
window = tk.Tk()
def disable():
button1.config(state="disabled")
button1=tk.Button(command=disable)
testbild=tk.PhotoImage(file="testbild.gif")
button1.image=testbild
button1.configure(relief="flat", image=testbild, height=180, width=180,
background="lightgreen", activebackground="lightgreen", bd=0)
button1.pack()
window.mainloop()
You may be able to change the color of the stipple by setting a background color at Button creation time:
button1=tk.Button(command=disable, bg='black')
self._window = tkinter.Tk()
self._window.option_add("*background", "green")
self._window.configure(background='green')
How can I instead of saying self._window.configure(background='green') say background = <something else>, maybe a tkinter.Canvas with drawings on it?
Just add a canvas that covers the whole window:
self.background = tkinter.Canvas(self._window)
self.background.pack(fill="both", expand=True)
Once you do that, you can draw anything you want on the background. From this point forward, just make sure all other widgets are children of the canvas rather than the root window.