by default, the buttons from ttk module, have a one pixel border. how can I delete this border? below an example code:
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("400x300")
style=ttk.Style()
style.configure("TButton", padding=0, background="#ffffff") # I colored the background border white to see it in the window.. but, I don't want it, I want to delete the border!
MyButton = ttk.Button(root, text="I have a white border.. how can I delet it?")
MyButton.place(x=16, y=16)
root.mainloop()
I don't want to change the border color in order to hide it in the window,I want to delete it.
Related
I'm trying to make an overlay for my screen with text but whenever I open a transparent tkinter screen with:
app.wm_attributes("-alpha", 0.1)
the labels and buttons are almost fully transparent too, is there a way to fix this?
As far as I know, tkinter doesn't support alpha ranges for individual widgets or colors.
But you can use -transparentcolor instead. This lets you choose a single color to apply full transparency to.
Note: -alpha and -transparentcolor are both toplevel attributes and, as such, will affect all child widgets added to the window.
The code below illustrates some effects of -transparentcolor.
import tkinter as tk
root = tk.Tk()
root['bg'] = 'grey'
root.attributes('-transparentcolor', 'grey')
lbl = tk.Label(text='Hello World!', font='Helvetica 36 bold', bg='green', fg='grey')
btn = tk.Button(text='Button', font='Helvetica 36', bg='grey', fg='blue')
canvas = tk.Canvas(bg='grey')
lbl.pack()
canvas.pack()
btn.pack()
root.mainloop()
My image "1_T.png" has a transparent background. I want to bring it to the front of my tkinter window.
I first tried inserting the image in a label. This brought the image to the front of the window but the transparent background was lost because labels can't have transparent backgrounds is my understanding.
I then tried using canvas.create_image(). This helped me preserve the transparent background of my image "1_T.png" but i can't work out how to bring it to the front. I tried canvas.tag_raise(). But that didn't work either.
For example, in the code below, i want to bring the image in front of the label.
Is there a way for me to do that?
import tkinter as tk
HEIGHT = 500
WIDTH = 1000
root = tk.Tk()
canvas = tk.Canvas(root, bg='black', height=HEIGHT, width=WIDTH)
canvas.pack()
mylabel = tk.Label(root, text='testing', bg='#00FFFF')
mylabel.place(relx=0, rely=0, relwidth=0.3, relheight=0.1)
myimage = tk.PhotoImage(file='1_T.png')
myimage2 = canvas.create_image(0, 0, image=myimage)
canvas.tag_raise(myimage2)
root.mainloop()
I am new to tkinter.
My code likes this,
import tkinter
from tkinter import scrolledtext
Win = tkinter.Tk()
Text = scrolledtext.ScrolledText(Win)
Text.pack(padx=10,pady=10)
Win.mainloop()
As you can see,it only have the left border,top border.
However,it haven't right border and bottom border.
I have watched this How to set border color of certain Tkinter widgets?.
And I have tried highlightbackground=color,
the code is this
import tkinter
from tkinter import scrolledtext
Win = tkinter.Tk()
Text = scrolledtext.ScrolledText(Win)
Text.config(highlightbackground="black")
Text.pack(padx=10,pady=10)
Win.mainloop()
it also didn't work.it made no difference.
In this document:tkinter.scrolledtext — Scrolled Text Widget,I know that the constructor is the same as that of the tkinter.Text class.And I have seen The Tkinter Text Widget,But it didn't have config about the tkinter.Text border.
What should I do?
it only have the left border,top border.
Because the widget config is relief="sunken",
So you can try relief="solid".
So this is may solve your problem
import tkinter
from tkinter import scrolledtext
Win = tkinter.Tk()
Text = scrolledtext.ScrolledText(Win,relief="solid")
Text.pack(padx=10,pady=10)
Win.mainloop()
I've been trying to configure the border colour of a widget in Tkinter, and I was wondering how to do that....
I've checked on StackOverflow, and it says that I should use the configure option and then set highlightbackgroundcolor = {insert color here}. I've tried that and nothing has worked. Can someone please show me a working sample of code so I can figure it out?
There is no way to change the border color of a widget, the border color is tied to the background color of the widget. Instead, you can turn off the border, and then use a frame widget where you can set the background color of the frame.
import tkinter as tk
root = tk.Tk()
label_border = tk.Frame(root, background="red")
label = tk.Label(label_border, text="This has a red border", bd=0)
label.pack(fill="both", expand=True, padx=1, pady=1 )
label_border.pack(padx=20, pady=20)
root.mainloop()
Is there any way to import an image into python using PIL and set it as the background for a ttk mainframe that spans the entirety of a Tkinter root window? As of now I have only seen ways to do this is a Tkinter root. Also is there any way to make ttk self adjust the size of the image so that even if it's small it covers the entire screen?
So to sum up I want an image to cover the entire ttk mainframe box without messing with me putting anything else in the ttk frame.
for example
if the pic covered the entire window, a command,
ttk.Button(root, text="Hello").grid(column=0, row=0, sticky=(N,S,W,E))
would still insert a button in the mainframe. Thanks :)
You can't set an image background to a ttk frame, they don't accept image options. So you could make a ttk frame and put a label or something inside it and then have it span the frame adapting the below example.
Here's a small example demonstrating what you want. We load an image with pil, notice the image linked will be smaller (I hope) than your screensize.
So, we set the geometry of the root window to be the entire screen the image is less than this so we resize it to cover the entire width. You can override the max and min height and then set it according to this. Just a sample value. We then place the bg label and grid widgets on top of it. The label has a lower stacking order than the other widgets you will place with grid so they appear on top. Alternatively you could use a canvas, or another widget. With a canvas you'll have to use create_window to place widgets in the canvas.
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
root = tk.Tk()
width, height = root.winfo_screenwidth(), root.winfo_screenheight()
#print(root.winfo_screenheight(), root.winfo_screenwidth())
root.geometry("%dx%d" % (width, height))
#URL FOR BACKGROUND
#https://www.google.com/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&ved=0ahUKEwiVroCiyZHMAhXKeT4KHQHpDVAQjBwIBA&url=http%3A%2F%2Fwallpaperswide.com%2Fdownload%2Fblack_background_metal_hole_very_small-wallpaper-800x480.jpg&psig=AFQjCNEjZ7GDbjG9sFie-yXW3fP85_p0VQ&ust=1460840934258935
image = Image.open("background.jpg")
if image.size != (width, height):
image = image.resize((width, height), Image.ANTIALIAS)
#print("DONE RESIZING")
# image.save("background.jpg")
#print(image.size)
image = ImageTk.PhotoImage(image)
bg_label = tk.Label(root, image = image)
bg_label.place(x=0, y=0, relwidth=1, relheight=1)
bg_label.image = image
your_button = ttk.Button(root, text='This is a button')
your_button.grid()
root.mainloop()