Python Forum
How add button to certain TAB using TAB-index
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How add button to certain TAB using TAB-index
#1
This is just a dummy-code, do not try it because it will probably not work. :)

Is´t possible to add 5 buttons to "tab1", next five to "tab2" and so on using for c in range(0, 20): in my example? I do not need to find out how to divide everything into parts of 5, that is my least problem. "tab1" marked with red is what I want using a variable to change.

buttons[c] = Button(tab1, text = "Button " + c ).place(x=c*100 , y=c*100)

Is´t possible to change tab1 = ttk.Frame(tabControl) and make tab to one array so I could use tab[1] for TAB-index 1? Is there any way to e.g. use tabControl [1]? Since I am a beginner, I have not really figured out how I can search for some answers yet. My Microsoft likes to say search "tkinter bind button to tabs index", but that's not what I want to do according to the search results. :)

Parts of my code so you can see how I create my TABS via Tkinter.

from tkinter import *
from tkinter import ttk
import tkinter as tk

root = tk.Tk()
root.title("MainWindow")
root.geometry("800x480")
root.configure(bg='black')
 
tabControl = ttk.Notebook(root)
tabControl.grid(row=1, column=0, columnspan=50, rowspan=49, sticky='NESW')
 
s = ttk.Style()
s.configure('TNotebook.Tab', font=('Helvetica','15','bold') )
 
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tab3 = ttk.Frame(tabControl)
tab4 = ttk.Frame(tabControl)
 
tabControl.add(tab1, text =' Start ')
tabControl.add(tab2, text =' Arbetsljus ')
tabControl.add(tab3, text =' Blåljus ')
tabControl.add(tab4, text =' Övrigt ')
 
tabControl.pack(expand = 1, fill ="both")

....

for c in range(0, 20):
    buttons.append(None)
    buttons[c] = Button(tab1, text = "Button " + c ).place(x=c*100, y=c*100)
....
This is just to show how I creat my TABS and how I add my buttons to TAB1. I´m not intrested how I can optimate this code, it´s just experiment to learn. ;)
Reply
#2
Here's a loop that makes the frames, adds them to the tab control and builds a list of tabs (frames).
tabs = []
for name in (' Start ', ' Arbetsljus ', ' Blåljus ', ' Övrigt '):
    frame = ttk.Frame(tabControl)
    tabControl.add(frame, name)
    tabs.append(frame)
The first frame is in tab[0], not tab[1].

It would make more sense if each tab was a class that was responsible for making it's own widgets. The code that makes a notebook should not have to know what is one any of the pages. If you are combining the making the pages with making the notebook you are going to end up with ugly code that is hard to modify. Ideally you should end up with something that looks like this:
tabControl = ttk.Notebook(root)
tabControl.add(StartPage(), ' Start ')
tabControl.add(ArbetsljusPage(), 'Arbetsljus')
...
The pages would be classes defined in other modules and you could add or remove pages as you see fit by just adding or removing an "add()" command. You could even automate populating the notebook by using a naming convention for the pages (like 'chapter_1.py', 'chapter_2.py'). The notebook program would search a folder files with names that match the pattern, import the file, create an instance and add the instance to the notebook.
Reply
#3
I agree with deanhystad.
Is this something what you are wanting. Tabs with buttons using a loop.
Each tab has five buttons numbered.

#! /usr/bin/env python3
import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.geometry('800x600+250+250')
root.title('Notebook')
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

# Create Notebook
style = ttk.Style()
current_theme = style.theme_use()
style.theme_settings(current_theme, {'TNotebook.Tab': {'configure': {'padding': [5, 5]}},
                                     'TNotebook': {'configure': {'tabmargins': [8, 8, 8, 8]}}
                                    })
notebook = ttk.Notebook(root)
notebook.grid(column=0, row=0, sticky='new')


frame1 = ttk.Frame(notebook)
frame1.grid(column=0, row=0, sticky='new')

frame2 = ttk.Frame(notebook)
frame2.grid(column=1, row=0, sticky='new')

frame3 = ttk.Frame(notebook)
frame3.grid(column=2, row=0, sticky='new')

frame4 = ttk.Frame(notebook)
frame4.grid(column=3, row=0, sticky='new')

tabs = {frame1: 'Tab 1', frame2: 'Tab 2', frame3: 'Tab 3', frame4: 'Tab 4'}

for key, val in tabs.items():
    notebook.add(key, text=val, padding=1)

frames = [frame1, frame2, frame3, frame4]

for i in range(1, 21):
    if i <= 5:
        frame = frame1
    elif i > 5 and i <= 10:
        frame = frame2
    elif i > 10 and i <= 15:
        frame = frame3
    else:
        frame = frame4
    btn = ttk.Button(frame, text=f'Button {i}')
    btn.grid(column=i, row=0, sticky='new', padx=2, pady=4)

root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
Thanks Deanhystad and Menator01.

It was just an example to put out 5 buttons on each tabs. In the end, I do not know how many buttons I will have on each TABS. I have a button array that I loop out the buttons from and info from the array so I want to be able to control which TAB it should end up on.

Deanhystad, that's how I wanted it to work. Just added tabControl.add (frame, text = name) :)

When I could have the TABS in an array i probably had solved it with % instead. Have not tested, but something like this to avoid a lot of IF and ELIF.
for i in range(1, 21):
    if (i % 5 == 0):
        t += 1
    buttons[i] = Button(tabs[ t ], text = "Button " + i ).place(x=i*100, y=i*100)
Thanks agin both. :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,012 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020