How to remove border of TTK Notebook? - python

Problem
My problem is that I need to remove the border for the ttk notebook. I am using ttk as a way of using multiple screens and I have removed the tabs from the notebook by doing: style.layout('TNotebook.Tab', []) but when I did that there is this ugly white border around the notebook this is what it looks like:
I am new to ttk so I do not know much about styling in the ttk module
So how can I remove the ugly white border of the notebook

I had a similar situation and wanted to remove the border surrounding Tkinter Notebook. Based on the suggestion from the comment #acw1668, I was able to remove the border. I post this answer incase it would help someone with similar problem.
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("400x300")
style=ttk.Style()
style.layout("TNotebook", [])
style.configure("TNotebook", highlightbackground="#848a98",tabmargins=0)# borderwidth = 0, highlightthickness = 0)
MainNotebook = ttk.Notebook(root, style="TNotebook")
MainNotebook.place(x=16, y=16)
Frame1=Frame(MainNotebook, background="#ffffff", width=200, height=150)
Frame1.pack()
Frame2=Frame(MainNotebook, background="#ffffff", width=200, height=150)
Frame2.pack()
MainNotebook.add(Frame1, text="Tab1")
MainNotebook.add(Frame2, text="Tab2")
root.mainloop()

Related

How do i resize this labelframe in tkinter?

So I tried to make this labelframe wider by using the basic width and width option.
Here's my given minimal code.
from tkinter import *
from tkinter import ttk
app = Tk()
app.resizable(False, False)
mainLayout = ttk.Frame(app, padding=10)
mainLayout.grid()
settings = ttk.Labelframe(mainLayout, text="Settings", padding=10, width=1000)
settings.grid()
ttk.Label(settings, text="Length limit (in seconds)").grid()
ttk.Spinbox(settings, from_=60, to=600, width=4).grid()
app.mainloop()
minimalized preview:
used in application:
i want to get this labelframe little bit bigger and make the inside centered, But i had no knowledge to do so, Any help will apreciated!
It seems like you just want to have a main_frame in the app. For simplicity I've used .pack with the options fill and expand with the constants tkinter.BOTH to stretch the widget in both (x,y) direction and True to consume extra space. (This is one of the reasons why wildcard imports are discouraged, you can be unaware of overwriting something, use import tkinter as tk instead). Same happens with the LabelFrame, you may could delete one of the containers, but that is up to you.
In LabelFrame I have configured the grid and gave the instruction that the column 0 should get the extra space with the priority/weight 1.
In addition, I gave your Spinbox a little bit more width, changed the size of the window and separated the constructor from the geometrymethod.
To get in touch with the geometry management in tkinter, you could play around with the instructions (e.g. comment some out) and see what happens.
from tkinter import *
from tkinter import ttk
app = Tk()
app.geometry('500x500')
app.resizable(False, False)
mainLayout = ttk.Frame(app, padding=10)
mainLayout.pack(fill=BOTH,expand=True)
settings = ttk.Labelframe(mainLayout, text="Settings", padding=10, width=1000)
settings.pack(fill=BOTH,expand=True)
settings.columnconfigure(0,weight=1)
my_label = ttk.Label(settings, text="Length limit (in seconds)")
my_label.grid()
my_spinbox = ttk.Spinbox(settings, from_=60, to=600, width=20)
my_spinbox.grid()
app.mainloop()

Change ttk Entry focus color - Python

I am trying to change the focus color of a ttk.Entry widget in python (blue border):
I know that we can change the focus color of the notebook tabs with style.configure('Tab', focuscolor='red'), so I wonder how to do it with an entry widget?
Here is my code:
import tkinter
from tkinter import ttk
root = tkinter.Tk()
style = ttk.Style()
style.theme_use('clam')
style.configure('TEntry', focuscolor='red') # don't work
ttk.Entry(root).grid(padx=10, pady=10)
root.mainloop()
As suggested by #Thingamabobs and #acw1668 It's possible to change the focus color of a ttk.Entry by mapping the focus state in its style property. Here is the working code:
import tkinter
from tkinter import ttk
root = tkinter.Tk()
style = ttk.Style()
style.theme_use('clam')
style.map('TEntry', lightcolor=[('focus', 'white')])
ttk.Entry(root).grid(padx=10, pady=10)
root.mainloop()
My final goal was to hide the focus border so I changed its color to white (the background color), and now the result looks like this: (The black border is just the original border of the entry)

Prevent scrolledtext from taking up entire parent window disallowing other widgets from showing up

So I am actually writing a simple GUI program which makes use of ScrolledText widget from tkinter.scrolledtext module.
The problem is this ScrolledText widget seems to take up the complete space available in the parent window. It disallows me from putting in any other widget in the same parent window. I have tried using both grid and pack GeoManagers (i know place isn't very useful in all cases), but the other widgets won't show up (neither above the scrolledtext widget nor below it).
HERE IS THE CODE--
import tkinter as tk
import tkinter.scrolledtext as sct
win2 = tk.Tk()
win2.geometry('1150x680')
win2.wm_geometry('+80+20')
txtbox = sct.ScrolledText(win2, width=500, height=350, bg='#fff', fg='#00f')
txtbox.grid(row=0, column=0)
txt = '<ABOUT 60 Lines TEXT HERE>'
txtbox.insert(1.0, txt)
txtbox.configure(state=tk.DISABLED)
tk.Button(win2, text='Got It', command=win2.destroy).grid(row=1, column=0)
This code is actually a part of a static method (i don't think makes a difference). When this is run the only thing visible on the screen is the scrolledtext widget with those 60 lines (i have tried it with 2 lines as well - still doesn't work).
The same happens when using pack().
To my surprise the only thing i could find in documentation is this::
ScrolledText Documentation
I don't know what I am missing here so please suggest me a way around this.
Thanks You :)
Solution with grid
The problem is the configuration of the grid: by default, the grid cells expand to fit the content. In your case the text widget is so big that the button in the row below is out of the screen. To fix that, you need to configure the first row and column to stretch with the GUI:
win2.rowconfigure(0, weight=1)
win2.columnconfigure(0, weight=1)
and make the text widget fill the cell, using the sticky option:
txtbox.grid(row=0, column=0, sticky='ewns')
This way the text widget will adapt to the window size and not the other way around.
Full code:
import tkinter as tk
import tkinter.scrolledtext as sct
win2 = tk.Tk()
win2.geometry('1150x680')
win2.wm_geometry('+80+20')
win2.rowconfigure(0, weight=1)
win2.columnconfigure(0, weight=1)
txtbox = sct.ScrolledText(win2, width=500, height=350, bg='#fff', fg='#00f')
txtbox.grid(row=0, column=0, sticky='ewns')
txt = '<ABOUT 60 Lines TEXT HERE>'
txtbox.insert(1.0, txt)
txtbox.configure(state=tk.DISABLED)
tk.Button(win2, text='Got It', command=win2.destroy).grid(row=1, column=0)
Alternative method, using pack
You can use pack with the options fill='both' and expand=True to achieve the same result as with grid. In this case, the additional trick is to pack the button first to ensure that it has enough space to show in the window. Code:
import tkinter as tk
import tkinter.scrolledtext as sct
win2 = tk.Tk()
win2.geometry('1150x680')
win2.wm_geometry('+80+20')
tk.Button(win2, text='Got It', command=win2.destroy).pack(side='bottom')
txtbox = sct.ScrolledText(win2, width=500, height=350, bg='#fff', fg='#00f')
txtbox.pack(fill='both', expand=True)
txt = '<ABOUT 60 Lines TEXT HERE>'
txtbox.insert(1.0, txt)
txtbox.configure(state=tk.DISABLED)

Make a Tkinter element on top of the others in Python

I'm searching for a way to put one element of Tkinter on top of the others in Python.
In the example below, what I tried to do is to put the yellow label on top of the white one - so when we change the size of the window the first label that will disappear is the white one, and only when its no longer visible the yellow one will start to shrink.
How can I realize this idea? Thanks in advance! (;
The code:
import tkinter as tk
root = tk.Tk()
root.config(background='black')
regularlabel = tk.Label(root, width=20, height=10, text='label')
regularlabel.pack()
bottomlabel = tk.Label(root, text='bottom label', bg='yellow')
bottomlabel.pack(side='bottom', fill='both')
root.mainloop()
Tkinter will shrink widgets in the reverse order of which they were added. Pack the bottom window first to solve your problem.
Since the layout of widgets tends to be changed more than the actual widgets during development, I find the code easier to understand and easier to maintain when layout is separate from widget creation. In the following example I've grouped the two layout statements together.
import tkinter as tk
root = tk.Tk()
root.config(background='black')
regularlabel = tk.Label(root, width=20, height=10, text='label')
bottomlabel = tk.Label(root, text='bottom label', bg='yellow')
bottomlabel.pack(side='bottom', fill='both')
regularlabel.pack()
root.mainloop()

How to remove indent left on the widget Tkinter Treeview?

Can I somehow remove this indentation? I know that I can remove the first column, but then I can not insert the image.
import tkinter as tk
import tkinter.ttk as ttk
from PIL import Image, ImageTk
class App:
def __init__(self, master):
self.tree = ttk.Treeview(master)
self.tree.heading('#0', text='Directory', anchor='w')
self.tree["columns"]=("num_files")
self.tree.heading('num_files', text='Number of files', anchor='w')
self.image_tk = ImageTk.PhotoImage(Image.open('icon.png'))
for i in range(10):
self.tree.insert('', 'end', text="Dir %d" % (i+1), values=(15), open=False, image=self.image_tk)
self.tree.pack(expand=1,fill="both")
root = tk.Tk()
app = App(root)
root.mainloop()
Assuming the indentation you're referring to is the left side of the first column (left of all the icons), you can adjust the entire widget padding as needed. For your application, start with:
self.tree = ttk.Treeview(master, padding=[-15,0,0,0])
What you have highlighted in red is the area in which the Treeview's indicator resides -- the open or close toggle icon -- that is shown if any of the Item objects contain subitems of their own.
One way to accomplish removing that area would be to just remove the indicator altogether while using a ttk theme that allows you to do so.
s = ttk.Style()
s.theme_use('default')
s.configure('Treeview.Item', indicatorsize=0)
This approach seems to only work for the default, clam and classic themes on Windows 10. For other themes, Ron's answer may be your best bet.

Categories

Resources