且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在python 3.4中使用tkinter添加2个滚动条?

更新时间:2023-02-21 09:36:18

I've also struggled with creating a double scrollbar window. Here is an implementation using pack layout manager (you should be able to tailor it for a grid layout) :

import tkinter as tk
from tkinter import ttk

# Top-level frame
root = tk.Tk()
root.title( "Double scrollbar with tkinter" )
root.minsize(width = 600, height = 600)
frame = ttk.Frame(root, relief="sunken")

# Canvas creation with double scrollbar
hscrollbar = ttk.Scrollbar(frame, orient = tk.HORIZONTAL)
vscrollbar = ttk.Scrollbar(frame, orient = tk.VERTICAL)
sizegrip = ttk.Sizegrip(frame)
canvas = tk.Canvas(frame, bd=0, highlightthickness=0, yscrollcommand = vscrollbar.set, xscrollcommand = hscrollbar.set)
vscrollbar.config(command = canvas.yview)
hscrollbar.config(command = canvas.xview)


# Add controls here
subframe = ttk.Frame(canvas)

#Packing everything
subframe.pack(padx   = 15, pady   = 15, fill = tk.BOTH, expand = tk.TRUE)
hscrollbar.pack( fill=tk.X, side=tk.BOTTOM, expand=tk.FALSE)
vscrollbar.pack( fill=tk.Y, side=tk.RIGHT, expand=tk.FALSE)
sizegrip.pack(in_ = hscrollbar, side = tk.BOTTOM, anchor = "se")
canvas.pack(side = tk.LEFT, padx  = 5, pady   = 5, fill = tk.BOTH, expand= tk.TRUE)
frame.pack( padx   = 5, pady   = 5, expand = True, fill = tk.BOTH)


canvas.create_window(0,0, window = subframe)
root.update_idletasks() # update geometry
canvas.config(scrollregion = canvas.bbox("all"))
canvas.xview_moveto(0) 
canvas.yview_moveto(0)


# launch the GUI
root.mainloop()

(Tested with Python 3.4).

Exemples :

It's not perfect, since it lacks the small block on the bottom right corner.

UPDATE 1 : Added Sizegrip to get the bottom right resizing corner

UPDATE 2 : Added working solution to OP's updated code

from win32com.client import Dispatch
from tkinter import *
from tkinter import ttk
import tkinter


class applicazione(object):


    def __init__(self, root):
        self.root = root

        # Top-level frame
        self.root.title('Controllo dati per fatturazione elettronica - Manticle - Smith & Nephew Italia')

        self.frame = ttk.Frame(self.root, width=300, height=250)

        # Canvas creation with double scrollbar
        hscrollbar = ttk.Scrollbar(self.frame, orient = tkinter.HORIZONTAL)
        vscrollbar = ttk.Scrollbar(self.frame, orient = tkinter.VERTICAL)
        sizegrip = ttk.Sizegrip(self.frame)
        self.canvas = tkinter.Canvas(self.frame, bd=0, highlightthickness=0, yscrollcommand = vscrollbar.set, xscrollcommand = hscrollbar.set)
        vscrollbar.config(command = self.canvas.yview)
        hscrollbar.config(command = self.canvas.xview)


        # Add controls here
        self.subframe = ttk.Frame(self.canvas)        


        #### Titles
        self.titoli_HFT = ["Dest_Cod_FIS_P_Iva", "Cod_Cli", "Dest_RagSoc1", "Dest_RagSoc2",
                           "Dest_Indirizzo", "Dest_CAP", "Dest_Localita", "Dest_Provincia", "Cli_Cod_Fis_P_Iva",
                            "Cod_Dest", "Cli_RagSoc1", "Cli_RagSoc2", "Cli_Indirizzo", "Cli_CAP",
                           "Cli_Localita", "Cli_Provincia", "Tipo_Doc", "pre_Num_Doc", "Num_Doc",
                            "Data_Doc", "Vs_Rif", "Data_Rif", "Blank", "Blank2", "CPT", "Scad_Pagamento",
                           "Descr", "Num_Ord", "Tot_Imp", "Tot_IVA", "Tot_Doc", "Nazione", "IPA", "Reg_Fiscale"]



        self.inizializza_widgets()

        #Packing everything
        self.subframe.pack(fill = tkinter.BOTH, expand = tkinter.TRUE)
        hscrollbar.pack( fill=tkinter.X, side=tkinter.BOTTOM, expand=tkinter.FALSE)
        vscrollbar.pack( fill=tkinter.Y, side=tkinter.RIGHT, expand=tkinter.FALSE)
        sizegrip.pack(in_= hscrollbar, side = BOTTOM, anchor = "se")
        self.canvas.pack(side = tkinter.LEFT, padx  = 5, pady  = 5, fill = tkinter.BOTH, expand= tkinter.TRUE)
        self.frame.pack( padx   = 5, pady  = 5, expand = True, fill = tkinter.BOTH)


        self.canvas.create_window(0,0, window = self.subframe)
        self.root.update_idletasks() # update geometry
        self.canvas.config(scrollregion = self.canvas.bbox("all"))
        self.canvas.xview_moveto(0) 
        self.canvas.yview_moveto(0)    


    def inizializza_widgets(self):

        self.LabelFrame = ttk.Frame(self.subframe)
        self.lblins = tkinter.Label(self.LabelFrame, text="Controllo dati per fatturazione elettronica", font=("Helvetica", 12))
        self.lblins.pack()

        for i in range (0,len(self.titoli_HFT)):
            j = 80*i
            self.lblnome=tkinter.Label(self.LabelFrame, text= self.titoli_HFT[i], font=("Helvetica", 8))
            self.lblnome.pack(side = LEFT)#place(x=85+j, y=50)

        self.ContentFrame = ttk.Frame(self.subframe, width = 600, height = 600)

        self.ButtonsFrame = ttk.Frame(self.subframe)
        ttk.Frame(self.ButtonsFrame).pack(side=LEFT, fill = X, expand=TRUE)
        ttk.Button(self.ButtonsFrame, text='Inserisci', width='10').pack(side = LEFT)
        ttk.Button(self.ButtonsFrame, text='Modifica', width='10').pack(side = LEFT)
        ttk.Button(self.ButtonsFrame,  text='Indietro', width='10').pack(side = LEFT)
        ttk.Button(self.ButtonsFrame,  text='Avanti', width='10').pack(side = LEFT)
        ttk.Frame(self.ButtonsFrame).pack(side=RIGHT, fill = X, expand=TRUE)

        self.LabelFrame.pack(side = TOP, fill = X, expand=TRUE)
        self.ContentFrame.pack(fill = BOTH, expand = TRUE)
        self.ButtonsFrame.pack(side = BOTTOM, fill = X, expand = TRUE)



if __name__ == '__main__':
    root = tkinter.Tk()
    root.title( "Double scrollbar with tkinter" )
    w, h = root.winfo_screenwidth(), root.winfo_screenheight()
    root.geometry("%dx%d+0+0" % (0.99*w, 0.9*h))
    applicazione(root)


    root.mainloop()