How to set tkinter Message border - python

I have tried all ways to set tkinter message widget border, but still receive any effect. I wonder if it does not have a border attribute? But I can set label and Text border using the same code. Here is my code snippet:
from tkinter import *
root = Tk()
frame = Frame(root)
message = Message(frame, text="hello world", width=200)
message.config(fg="red", borderwidth=100, highlightcolor="green")
frame.pack()
message.pack()
root.minsize(300, 200)
root.mainloop()
this's result:
OS Version:OS X 10.11.4
Python version: 3.52

You are setting the borderwidth correctly. The issue is that you can't see the changes you are implementing, until your add background colour and change the relief of the Frame and Message widget. Now try changing the borderwidths with this revised code, and I trust you get the "gotcha" moment.
from tkinter import *
root = Tk()
frame = Frame(root, background='yellow', borderwidth=20, relief=RAISED)
message = Message(frame, text="hello world", width=200)
message.config(fg="red", borderwidth=50, highlightcolor="green",
background='light blue', relief=SUNKEN)
frame.pack()
message.pack()
root.minsize(300, 200)
root.mainloop()

Related

Not Able to See the Picture in Tkinter Python Program

topper = Toplevel()
topper.title("2nd Window")
topper.state('zoomed')
my_img = ImageTk.PhotoImage(Image.open("diamond.png"))
my_label = Label(topper, image=my_img, height = 100 , width = 100)
F21 = Frame(topper, borderwidth=83, bg="blue", relief=SUNKEN)
button1 = Button(topper, text="class", command=topper.destroy)
button1.pack()
my_label.pack()
I am running the code and I get no errors, button is working as well but I'm not able to see the picture in the window.
Welcome to Stackoverflow Shakti!
For the future - it is always good to supply a Minimal, Reproducible Example so others can replicate and understand your issue better in order to help you! It also helps you to understand where exactly the error originates.
When you call Toplevel() from another tkinter window to open a new window, you will need to call "mainloop()" on the second window as well in order to display an image - try my code with an example image and comment/uncomment the line with
topper.mainloop()
to see the difference in functionality.
The adapted code:
from tkinter import *
from PIL import Image, ImageTk # pip install pillow
def show_second_window():
topper = Toplevel()
topper.title("2nd Window")
topper.state('zoomed')
my_img = ImageTk.PhotoImage(Image.open("t1.png"))
my_label = Label(topper, image=my_img, height=100, width=100)
F21 = Frame(topper, borderwidth=83, bg="blue", relief=SUNKEN)
button1 = Button(topper, text="class", command=topper.destroy)
button1.pack()
my_label.pack()
topper.mainloop() # <---- this is needed to show the image!
root = Tk()
root.title("1st window")
button = Button(root, text="show second window", command=show_second_window)
button.pack()
root.mainloop()

Adding an image to a button in Tkinter

I am trying to add an image to a button, but I have got some issues when I try to execute the current code. All it shows is an image with no words. I can't even see the button either. Is there some way of fixing my current code?
from tkinter import *
import tkinter as tk
root = tk.Tk()
root.geometry("960x600")
canvas = Canvas(root, width=500, height=500)
canvas.pack()
imagetest = PhotoImage(file="giftest.gif")
canvas.create_image(250, 250, image=imagetest)
button_qwer = Button(root, text="asdfasdf", image=imagetest)
root.mainloop()
You need to pack (or grid) your button in the window, here is how you could do:
import tkinter as tk
from tkinter import PhotoImage
def print_hello():
print('hello')
root = tk.Tk()
root.geometry("960x600")
imagetest = PhotoImage(file="giftest.gif")
button_qwer = tk.Button(root, text="asdfasdf", image=imagetest, command=print_hello)
button_qwer.pack() # <-- don't forget to place the button in the window
root.mainloop()
You can have both text and image displayed on your button, using the compound option, like this:
button_qwer = tk.Button(root, image=imagetest, text="asdfasdf", compound="top", command=print_hello)
compound options are bottom, center, left, none, right, or top
You are making the button successfully but you are not drawing it onto the screen/interface. Use pack , place or grid.
button_qwer = Button(root, text="asdfasdf", image=imagetest)
button_qwer.pack()
Your full code can be like:
from tkinter import *
import tkinter as tk
root = tk.Tk()
root.geometry("960x600")
canvas = Canvas(root, width=500, height=500)
canvas.pack()
imagetest = PhotoImage(file="giftest.gif")
canvas.create_image(250, 250, image=imagetest)
button_qwer = Button(root, text="asdfasdf", image=imagetest)
button_qwer.pack()
root.mainloop()

Tkinter's button can't change border color

Here's my Tkinter code:
Photoshop = Tkinter.Button(root,
text = 'Photoshop',
fg = '#37d3ff',
bg = '#001d26',
bd = 10,
highlightthickness=4,
highlightcolor="#37d3ff",
highlightbackground="#37d3ff",
borderwidth=4)
However, after I grid my Button, the color of border doesn't shows up. Instead, it used default grey.
You may place the button inside its own frame like this:
buttonborder = Tkinter.Frame(root,
highlightbackground="#37d3ff",
highlightcolor="#37d3ff",
highlightthickness=4,
bd=0)
photoshop = Tkinter.Button(buttonborder,
text='Photoshop',
fg='#37d3ff',
bg='#001d26')
This works for me:
import Tkinter as tk
root = tk.Tk()
Photoshop = tk.Button(root, text = 'Photoshop',
fg = '#37d3ff',
bg = '#001d26',
bd = 10,
highlightthickness=4,
highlightcolor="#37d3ff",
highlightbackground="#37d3ff",
borderwidth=4)
Photoshop.pack()
root.mainloop()
You can add your widget to a Frame and make the Frame's highlight background to be the color you want for your widget's border. CODE Example:
import tkinter as tk
root = tk.Tk()
buttonborder = tk.Frame(root, highlightbackground="#37d3ff",
highlightthickness=3, bd=0)
photoshop = tk.Button(buttonborder, text='Photoshop', fg='#37d3ff')
photoshop.pack()
buttonborder.pack()
root.mainloop()
You can do it with LabelFrame() and relief.
Works in windows.
from tkinter import *
App = Tk()
Border = LabelFrame(App,
bd=5, #<- Borderwidth.
bg="blue", #<- Border color.
relief=FLAT)
Border.pack(padx=10, pady=10)
Btn1 = Button(Border, #<- in Border Widget.
text="Button",
font="Arial 16 bold",
width=16,
bg="red",
fg="white",
relief=FLAT)
Btn1.pack()
App.mainloop()
There is no perfect way to do this unfortunately, But you sure can hack around and place a tkinter Frame that is just a little bigger than the actual button to set them apart by using the frame as a coloured border, Something like this. Should work on Win and Mac or any other OS's..
(assuming you already know how to work with tkinter root window..)
`borderFrame = Frame(root, bg="red(your desired colour of choice)")
borderFrame.pack(padx=21, pady=21)
button = Button(borderFrame, bg="blue",text="click me", relief='flat')
button.pack(padx=20, pady=20)`

How to get rid of widget border?

I have the following code:
from Tkinter import *
def gui():
root = Tk()
root.configure(background = 'red')
rightPanel = PanedWindow(borderwidth=0, bg='black')
rightPanel.pack(side = 'right', fill=BOTH, expand=1)
canvas1 = Canvas(rightPanel, bg='black')
rightlabel = Label(canvas1, bg= 'grey')
rightlabel.place(relx=0.5, rely=0.5, anchor=CENTER)
canvas1.pack(fill=BOTH, expand=1)
root.wm_attributes('-topmost', 1)
mainloop()
if __name__ =='__main__':
gui()
As you can see if you run it (especially in fullscreen mode), there is grey border near window edge.
It looks like border of PanedWindow widget (you can see it, if you set its fill=NONE and expand window). Note that ts borderwidth is set to 0
How can I get rid of it or set it to some color?
What you are seeing is the highlight ring around the canvas -- something that changes color to show that the canvas has keyboard focus. Set it to zero with the highlightthickness attribute:
canvas1 = Canvas(rightPanel, bg='black', highlightthickness=0)
Note that it could also be the canvas border. You might want to set borderwidth to zero, too.

tkinter canvas image is not displayed in class

I am trying to display an image in python using the tkinter canvas option. However, if I input it in a class, like below, it doesn't give an error but also doesn't show my image. The buttons are displayed correctly though. Also, if I take the code for generating this image out of the class it works correctly. I can't seem to find out what the problem is.
import Tkinter as tk
from Tkinter import *
class Board(tk.Frame):
def __init__(self,parent):
frame = Frame(parent)
frame.pack()
tk.Frame.__init__(self,parent)
frame2 = Frame(frame)
frame2.pack()
c=Canvas(frame2)
c.pack(expand=YES,fill=BOTH)
background=PhotoImage(file='Board.gif')
c.create_image(100,100,image=background,anchor='nw')
button = Button(frame, text="Next turn", command=self.next_turn)
button.pack()
button = Button(frame, text="Roll the dice", command=self.roll)
button.pack()
....
root = Tk()
board = Board(root)
board.pack()
root.mainloop()
You have to keep a reference to the PhotoImage. This is just and example (you can also use self.background instead of c.background):
c = Canvas(frame2)
c.pack(expand=YES,fill=BOTH)
c.background = PhotoImage(file='Board.gif')
c.create_image(100,100,image=c.background,anchor='nw')

Categories

Resources