[PYTHON] Tab app in Tkinter

Introduction

Create a simple app with tabbed windows in Tkinter. Introduced a tabbed window to the "editor" style app created in the previous "Easy GUI app with Tkinter Text" to edit multiple files. Make it possible. Use the Notebook widget of the tkinter.ttk module to create a tabbed window.

Complete image

It is an "editor" style application that uses a tabbed window. tabedit.png

Commentary

import

Import tkinter and tkinter.ttk.

import tkinter as tk
import tkinter.ttk as ttk

Create Notebook

Create a Notebook and place it in the app window.

root = tk.Tk()
notebook = ttk.Notebook(root)
notebook.pack(fill='both',expand=1)

Add tab

Create a frame to place on the tab and add the tab with Notebook.add (). If you leave it as it is, the added tab is hidden behind it, so use Notebook.select () to bring it to the front. Notebook.tabs () returns a tab_id list and Notebook.index ('end') returns the number of tabs, so reduce the number of tabs by -1 and pass the last tab_id in the list to Notebook.select ().

frame=tk.Frame(notebook)
notebook.add(frame,text='title')
notebook.select(notebook.tabs()[notebook.index('end')-1])

Selected tab

Returns the tab_id of the tab selected by Notebook.select () with no arguments. You can check the number of tabs added by using the tab_id selected for the index () method of the tab_id list returned by Notebook.tabs () as an argument.

idx=notebook.tabs().index(notebook.select())

All source code

import os
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog

class SbTextFrame(tk.Frame):
    def __init__(self,master):
        super().__init__(master)
        text = tk.Text(self,wrap='none',undo=True)
        x_sb = tk.Scrollbar(self,orient='horizontal')
        y_sb = tk.Scrollbar(self,orient='vertical')
        x_sb.config(command=text.xview)
        y_sb.config(command=text.yview)
        text.config(xscrollcommand=x_sb.set,yscrollcommand=y_sb.set)
        text.grid(column=0,row=0,sticky='nsew')
        x_sb.grid(column=0,row=1,sticky='ew')
        y_sb.grid(column=1,row=0,sticky='ns')
        self.columnconfigure(0,weight=1)
        self.rowconfigure(0,weight=1)
        self.text = text
        self.x_sb = x_sb
        self.y_sb = y_sb

def add_tab(fname):
    global tframes,fnames,notebook
    tframe=SbTextFrame(notebook)
    tframes.append(tframe)
    if os.path.isfile(fname):
        f=open(fname,'r')
        lines=f.readlines()
        f.close()
        for line in lines:
            tframe.text.insert('end',line)
    fnames.append(fname)
    title=os.path.basename(fname)
    notebook.add(tframe,text=title)
    notebook.select(notebook.tabs()[notebook.index('end')-1])

def fileopen():
    fname = filedialog.askopenfilename()
    add_tab(fname)

def filesave():
    global tframes,fnames,notebook
    idx = notebook.tabs().index(notebook.select())
    fname = fnames[idx]
    tframe = tframes[idx]
    f = open(fname,'w')
    f.writelines(tframe.text.get('1.0','end-1c'))
    f.close()

def main():
    global root,notebook,tframes,fnames
    root = tk.Tk()
    root.title('tabbed editor')
    root.geometry('400x300')
    notebook = ttk.Notebook(root)
    notebook.pack(fill='both',expand=1)
    tframes = []
    fnames = []
    add_tab('new')
    menubar = tk.Menu(root)
    filemenu = tk.Menu(menubar,tearoff=0)
    filemenu.add_command(label='Open',command=fileopen)
    filemenu.add_command(label='Save',command=filesave)
    filemenu.add_command(label='Exit',command=exit)
    menubar.add_cascade(label='File',menu=filemenu)
    root.config(menu=menubar)
    root.mainloop()

if __name__ == '__main__':
    main()

in conclusion

This time, I made an "editor" style application that uses a tabbed window. When using multiple windows, not just the "editor", it's nice to use tabs to make the screen cleaner.

Recommended Posts

Tab app in Tkinter
Embed matplotlib graphs in Tkinter
[Python] Show multiple windows in Tkinter
Implement a date setter in Tkinter
Put the module in the Pepper app
Easy GUI app with Tkinter Text
GUI creation in python using tkinter 2
GUI creation in python using tkinter part 1
Creating an image splitting app with Tkinter
Create a simple GUI app in Python
Create a GUI app with Python's Tkinter
Create a Python-GUI app in Docker (PySimpleGUI)
Launch a Flask app in Python Anywhere
Tkinter could not be imported in Python
Put Tkinter in Macbook and check operation