Placing tkinter widgets in foreground - python

I cannot achieve to create a Textbox (using tkinter's Text widget) which is in the background and having a Canvas item (e.g. an oval) over it (foregroud) :
import Tkinter as Tk
root = Tk.Tk()
c = Tk.Canvas(root, width=400, height=400, bg='white')
o = c.create_oval(10, 10, 390, 390, fill='red')
c.grid(row=0, column=0, columnspan=2, padx=5, pady=5)
t = Tk.Text(c)
t.place(x=10,y=10)
c.tag_lower(t)
c.tag_raise(o)
root.mainloop()
Even if I use c.tag_lower(t), c.tag_raise(o), it doesn't work. Do you know how to solve this problem ?

This is documented behavior. You cannot draw over other widgets that are embedded in a canvas. There is no workaround. To be able to draw on top of text, your only option is to draw the text using create_text.

Related

Tkinter: Placing widgets on top of other Widgets

I'm trying to place an Entry widget on top of a frame but i'm having some trouble with it
(I'm using customtk but i suppose there's no big difference)
import customtkinter
app = customtkinter.CTk()
app.geometry(500x380)
random_frame = customtkinter.CTkFrame(master=app, width=480, height=50)
Box1 = customtkinter.CTkEntry(master=app, width=30, height=30)
random_frame.grid(row=3, column=0, padx=10, pady=10)
Box1.grid(row=3, column=1, padx=10, pady=10)
I am expecting the Box1 widget to be on top of the random_frame widget, however in the gui i can't see the Box1. I'm unsure if it's under the frame, but that's what i think is happening.
I tried using the lift() method but it doesn't seem to do anything

One of my canvases doesn't show in the Tkinter window (Python)

I'm trying to make a Tkinter window with two canvases: one for inputs, one for results.
The reason for that is that I couldn't find a way to clear just the results when the next input comes in so if there's a way to do that, that would also solve my problem, but I didn't find a way to solve that.
The issue is that the second canvas won't show up on the window. I've tried putting something on it right away, but it still didn't work. Also when searching for a solution, I found a code that I tried for myself and then it worked.
Here's the code:
root = tk.Tk() #I used import tkinter as tk
canvas = tk.Canvas(root, width = 400, height = 200)
canvas.pack(side='top', anchor='nw', fill='x')
canvas2 = tk.Canvas(root, width = 400, height = 600)
canvas.pack(side='top', anchor='nw', fill='both')
You typed canvas.pack(side='top', anchor='nw', fill='both') instead of canvas2.pack(side='top', anchor='nw', fill='both'). Here is the corrected code:
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=400, height=200, bg="red")
canvas.pack(side='top', anchor='nw', fill='x')
canvas2 = tk.Canvas(root, width=400, height=600, bg="blue")
canvas2.pack(side='top', anchor='nw', fill='both')
root.mainloop()

Tkinter - uneven frames overlapping

Could someone please confirm if there is any possibility to create Tkinter frames with uneven dimensions overlapping, and then by using Tkinter raise function, I would like to display whichever frames is required to me, but for some reason, I couldn't find an option to do this, any suggestions/advises are much appreciated.
Something like the below in the image, yellow, green & red like to be 3 different frames
Thank you in advance..!!
Yes, this is possible, though not with transparency in the frames. If you don't really need frames and just need rectangles, this answer shows how to get transparency with images on a canvas.
Your post is unclear as to exactly what you expect, but here's an example that uses place to arrange the frames like in your picture. If you run the code, you can click on a frame to raise it to the top.
import tkinter as tk
root = tk.Tk()
root.geometry("800x800")
yellow_frame = tk.Frame(root, width=800, height=300, background="yellow", bd=8, relief="solid")
green_frame = tk.Frame(root, width=800, height=300, background="green", bd=8, relief="solid")
red_frame = tk.Frame(root, width=200, height=800, background="red", bd=8, relief="solid")
yellow_frame.place(x=0, y=0, anchor="nw")
green_frame.place(x=0, y=300, anchor="nw")
red_frame.place(x=500, y=0, anchor="nw")
for frame in (yellow_frame, green_frame, red_frame):
frame.bind("<1>", lambda event: event.widget.lift())
root.mainloop()

Tkinter Frame Disappears after adding a button [duplicate]

This question already has an answer here:
How to stop Tkinter Frame from shrinking to fit its contents?
(1 answer)
Closed 4 years ago.
I have two frames in the root. And I want to add a button in one of the frames. Both frames have different background colours. When I try to add a button in any of them, the frame that contains the button disappears.
Without button
from tkinter import *
root = Tk()
root.geometry("1600x800+0+0")
root.title("ABC")
Rf = Frame(root, width=100, height=800, bg="black")
Rf.pack(side=RIGHT)
Lf = Frame(root, width=1500, height=800, bg="green")
Lf.pack(side=LEFT)
root.mainloop()
Which results in...
With button
But after adding the button, with the following code...
from tkinter import *
root = Tk()
root.geometry("1600x800+0+0")
root.title("ABC")
Rf = Frame(root, width=100, height=800, bg="black")
Rf.pack(side=RIGHT)
Lf = Frame(root, width=1500, height=800, bg="green")
Lf.pack(side=LEFT)
b1 = Button (Rf, text="Load", fg="red", bg="black")
b1.pack(side=LEFT)
root.mainloop()
I get...
Now the button is visible but the frame and background colours are gone. What am I doing wrong?
Thanks for your help!
Your frame is actually disappearing it is just resize and that is why you cant see it. Add Rf.pack_propagate(False) to your frame it will prevent the frame from resizing when a new widget is added.
from tkinter import *
root= Tk()
root.geometry("1600x800+0+0")
root.title("ABC")
Rf=Frame(root,width=100, height=800, bg="black")
Rf.pack_propagate(False)
Rf.pack(side=RIGHT)
Lf=Frame(root,width=1500, height=800, bg="green")
Lf.pack(side=LEFT)
b1 = Button (Rf, text= "Load", fg= "red", bg="black")
b1.pack(side=LEFT)
root.mainloop()

get rid of blank space around widgets in tkinter

I put widgets on a window with .grid and noticed that the widgets have space around them. I fixed that on canvas widget with highlightthickness but this doesn't work with button widgets. It doesn't look cool when you have grey window background and there's white space around buttons.
P.S. I use python3 on macos Sierra
root = Tk()
root.configure(bg='grey')
w = Canvas(root, width=150, height=150, highlightthickness=0)
w.grid(row=1, column=1, sticky="nsew")
clear_btn = Button(text="Clear", width=15, command=lambda: w.delete("all"))
clear_btn.grid(row=2, column=1)
root.mainloop()
P.S. TKinter leaving borders around widgets that's what it looks like. The guy was advised to use highlightbackground, but when I screenshot widgets, I still have that blank space around the widget.
To get the button to fill the entire space of the grid box it occupies, try setting the sticky argument of the grid() function to "NSEW":
from tkinter import *
root = Tk()
root.configure(bg = "grey")
w = Canvas(root, width = 150, height = 150, highlightthickness = 0)
w.grid(row = 1, column = 1, sticky = "NSEW")
clear_btn = Button(text = "Clear", width = 15, command = lambda: w.delete("all"))
clear_btn.grid(row = 2, column = 1, sticky = "NSEW")
root.mainloop()
Before:
After:
Note: The appearance of these windows may vary slightly between operating systems and even between different visual themes on the same operating system.
I don't have OSX, so I cannot check, but I think that if you use ttk and a style different from the default one (like 'clam'), you might be able to configure things like you want.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.configure(bg='grey')
style = ttk.Style(root)
style.theme_use('clam')
w = tk.Canvas(root, width=150, height=150, highlightthickness=0)
w.grid(row=1, column=1, sticky="nsew")
clear_btn = ttk.Button(text="Clear", width=15, command=lambda: w.delete("all"))
clear_btn.grid(row=2, column=1)
root.mainloop()

Categories