Setting ttk Notebook tabs in the center [Python] - python

I want to make my own style in a python / tkinter application using ttk Notebooks. I like the style of the aqua setting shown below.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
s = ttk.Style(root)
s.theme_use('aqua')
notebook = ttk.Notebook(root)
frame1 = ttk.Frame(root, width=400, height=400)
frame2 = ttk.Frame(root)
notebook.add(frame1, text="Frame1")
notebook.add(frame2, text="Frame2")
notebook.pack()
root.geometry("400x400")
root.mainloop()
But aqua will not let me change the background color of some widgets so I want to use the default style. Is there a way to configure the TNotebook.Tab using the default style to get the tabs in the middle? Something like
s.configure('TNotebook.Tab', tabposition='center')
but I have not found anything that works.

I couldn't use theme aqua on Windows, but use default theme and
s.configure("TNotebook", tabposition='n')
could make the tab in the center.

Related

Tkinter scrollbar is not scrolling

As part of a tkinter app I'm building using Python 3.8, I need a particular tab in a Notebook to be scrollable. The notebook needs to remain at a fixed size, but the problem is that there will be cases in which the contents of the tab will exceed the size of the notebook.
The scrollbar appears as it should, but scrolling appears to have no effect on the contents of the tab. It looks like it thinks it's scrolling something but I do not know what. Here's an isolated example of a tab with a scrollbar which has no effect:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
tabs = ttk.Notebook(root, width=200, height=650)
tab_options = tk.Frame(tabs)
tabs.add(tab_options, text="Options")
main_frame = tk.Frame(tab_options)
main_frame.pack()
canvas = tk.Canvas(main_frame)
canvas.pack(side="left",fill="both",expand=1)
scrollbar = ttk.Scrollbar(main_frame,orient="vertical",command=canvas.yview)
scrollbar.pack(side="right", fill="y",expand=1)
lf_options = tk.Frame(canvas)
lf_options.pack()
canvas.configure(yscrollcommand=scrollbar.set)
canvas.configure(scrollregion=(0,0,200,1000))
for i in range(50):
ttk.Label(lf_options, text=str(i)).pack()
tabs.pack()
root.mainloop()
I imagine it's something to do with how I'm hooking up the frames to the canvas but I cannot for the life of me get it to work. I've seen suggestions about setting scrollregion to
canvas.bbox("all")
but I don't understand how to associate that with the maximum height that can be displayed, i.e. the height of the notebook itself. Using that as the scrollregion also just makes the scrollbar unscrollable.
I know there are many similar questions on here, but I have not found any of those examples to work in this case.
Any advice would be greatly appreciated. Thanks!
It seems more logical to use a tk.Listbox for this purpose, see below for example an edited version of your code. Here the scrollbar works just as expected!
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
tabs = ttk.Notebook(root, width=200, height=650)
tab_options = tk.Frame(tabs)
tabs.add(tab_options, text="Options")
listbox = tk.Listbox(tab_options)
for i in range(50):
listbox.insert(tk.END, f"Number {i}")
listbox.pack(side="left", fill="both", expand=1)
scrollbar = ttk.Scrollbar(tab_options, orient="vertical", command=listbox.yview)
scrollbar.pack(side="right", fill="y", expand=1)
listbox.configure(yscrollcommand=scrollbar.set)
tabs.pack()
root.mainloop()

Python Tkinter color not working, it is the same whether I change the customization with canvas

I apologize in advance for any ignorant mistakes I make, I am still a beginner.
I recently started using tkinter on python, and while I tried using the backround customization in tkinter, It does not change any colors in the backround, the same goes with the frame. below is the code I have written.
import tkinter
import tkinter as tk
class GUI:
root = tkinter.Tk()
canvas = tk.Canvas(
height=700,
width=700,
bg="#263D42"
)
canvas.config(bg="green")
canvas.pack()
root.mainloop()
frame = tk.Frame(root, bg="white")
frame.place(relheight=0.8, relwidth=0.8)
I am using M1 MacOS if that information helps, here is a screenshot of the GUI, it is the same when I comment out the color customization in the code.

Tkinter button color

When I create a button like this in tkinter:
self.submition_button=Button(self.root, text='Submit',
font='Times 12 bold italic', command=self.onSubmition, bg='blue')
the button isn't blue.
Why isn't it blue, and what can I do to make it blue?
Are you using tkinter.Button or are you really using ttk.Button?
You'll know which one your using based on what you imported in the top of code
regular tkinter widgets importing
import tkinter
typical TTK widgets importing
from tkinter import ttk
Because TTK is more modern version of the library it's styling system is different. So setting flag options on it wont work. instead you have to use style theme.
See this reply.
https://stackoverflow.com/a/44416355/8661716
This is the code for a blue button, check it :
from tkinter import *
#create a window
root =Tk()
button=Button(root, text='Submit', font='Times 12 bold italic', bg='blue', activebackground="blue")
button.pack()
root.mainloop()

How do I change ttk.LabelFrame's header label to blue in python's tkinter on Linux?

I want the label text to be blue, as it is by default on Windows.
I'm following a tutorial on Python and Tkinter that was written for Windows, but I'm doing it on Linux (Python 3.5.1 on xubuntu 16.04.5). Right now I'm puzzled by the fact that all the LabelFrame examples in the book have a blue header label, but when I run their code, my header labels are black.
I see from this question that blue is default in Windows. I'd like to see the same effect in Linux. However, I have been unable to reverse the suggestions there. For instance, "foreground" does not appear to be an option for ttk.LabelFrame(), and I also don't know how to make the text of a Label to be blue to use it as the labelwidget option.
Here's the code:
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("My question")
lf = ttk.LabelFrame(root, text="Why is this not blue?")
lf.pack()
label = ttk.Label(lf, text="frame contents can remain black or whatever.")
label.pack()
root.mainloop()
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("My question")
lw = Label(root, text="This is blue now", fg="blue")
lf = ttk.LabelFrame(root,labelwidget=lw)
lf.pack()
label = ttk.Label(lf, text="frame contents can remain black or whatever.")
label.pack()
root.mainloop()
I wrote the answer but didn't notice you're using LabelFrame instead of Label.
As the documentation says, you need to pass an options argument to your LabelFrame class.
Try this:
lf = ttk.LabelFrame(root, fg='blue', text="Now it should be blue")

Tkinter : How to center the window title

I am creating a project using tkinter and when I create a window, I couldn't seem to get the window title to center itself (Like most programs nowadays). Here's the example code:
from tkinter import *
root = Tk()
root.title("Window Title".center(110))# Doesn't seem to work
root.mainloop()
Is there a way to center the window title up ? Thanks in advance
There is nothing you can do. Tkinter has no control over how the window manager or OS displays the titles of windows other than to specify the text.
I came up with a trick that does the job and it consists in simply adding as much blank space before the title:
import tkinter as tk
root = tk.Tk()
root.title(" Window Title")# Add the blank space
frame = tk.Frame(root, width=800, height=200, bg='yellow')
frame.grid(row=0,column=0)
root.mainloop()
Output:
Alternatively, you can use a string consisting of an empty space and concatenate it to the title after multiplication. I mean:
import tkinter as tk
root = tk.Tk()
blank_space =" " # One empty space
root.title(80*blank_space+"Window Title")# Easier to add the blank space
frame = tk.Frame(root, width=800, height=200, bg='yellow')
frame.grid(row=0,column=0)
root.mainloop()
More adding onto what Billal suggested is this example that adjust depending on the window size. I still wouldn't recommend it since it's just a hack for visual aesthetics but if you really want to have it.
import tkinter as tk
def center(e):
w = int(root.winfo_width() / 3.5) # get root width and scale it ( in pixels )
s = 'Hello Word'.rjust(w//2)
root.title(s)
root = tk.Tk()
root.bind("<Configure>", center) # called when window resized
root.mainloop()
width=root.winfo_screenwidth()
spacer=(" "*(int(width)//6))
root.title(spacer+"Your title")
This is not that much perfect but this will work.

Categories