1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #!/usr/bin/python3
# -*- coding: utf-8 -*-
from tkinter import *
import os
from PIL import Image, ImageTk
from tkinter import Scrollbar, RIGHT, Y
from tkinter import Tk, Frame, Menu, Button, Text, E, W, S, N, WORD, ttk, NW
from tkinter import LEFT, TOP, X, FLAT, RAISED, BOTH, END
class AutoScrollbar(Scrollbar):
# a scrollbar that hides itself if it's not needed. only
# works if you use the grid geometry manager.
def set(self, lo, hi):
if float(lo) <= 0.0 and float(hi) >= 1.0:
self.grid_forget()
else:
self.grid(row=0, column=1, sticky=N+S)
Scrollbar.set(self, lo, hi)
def pack(self, **kw):
raise TclError ("cannot use pack with this widget")
def place(self, **kw):
raise TclError ("cannot use place with this widget")
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# Add the textbox
# Defines and places the notebook widget
self.nb = ttk.Notebook(self.master)
#self.self.nb.grid(row=0, column=0, columnspan=50, rowspan=49, sticky='NESW')
# Adds tab 1 of the notebook
page1 = ttk.Frame(self.nb)
self.nb.add(page1, text='Texte')
# Add the textbox
self.tbox1 = Text(page1, wrap=WORD)
self.tbox1.pack(fill=BOTH, expand=1)
# Add the scrollbar
scrollbar = Scrollbar(self.tbox1)
#scrollbar = AutoScrollbar(self.tbox1)
scrollbar.pack(side=RIGHT, fill=Y)
self.tbox1.pack()
# attach self.tbox1 to scrollbar
self.tbox1.config(yscrollcommand=scrollbar.set, borderwidth=10)
scrollbar.config(command=self.tbox1.yview)
self.tbox1.insert(0.0, 1000*"blablabla")
self.nb.pack(fill=BOTH, expand=1)
def main():
root = Tk()
root.geometry("850x650+100+100")
app = Example()
root.mainloop()
if __name__ == '__main__':
main() |
Partager