Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- import tkinter.ttk as ttk
- lorem_ipsum = 'Lorem ipsum dolor sit amet, luctus non. Litora viverra ligula'
- class Scrollbar(ttk.Scrollbar):
- def __init__(self, parent, canvas, **kwargs):
- ttk.Scrollbar.__init__(self, parent, **kwargs)
- command = canvas.xview if kwargs.get('orient', tk.VERTICAL) == tk.HORIZONTAL else canvas.yview
- self.configure(command=command)
- def set(self, low, high):
- if float(low) > 0 or float(high) < 1:
- self.grid()
- else:
- self.grid_remove()
- ttk.Scrollbar.set(self, low, high)
- class App(tk.Tk):
- def __init__(self):
- super().__init__()
- self.rowconfigure(0, weight=1)
- self.columnconfigure(0, weight=1)
- self.title('Paned Window Demo')
- self.geometry('420x200')
- style = ttk.Style()
- style.theme_use('clam')
- style.configure('TPanedwindow', background='black')
- pw = ttk.PanedWindow(self, orient=tk.HORIZONTAL)
- left_frame = ttk.Frame(pw)
- right_frame = ttk.Frame(pw)
- ttk.Label(left_frame, text='Left Pane').grid()
- left_frame.rowconfigure(0, weight=1)
- left_frame.columnconfigure(0, weight=1)
- left_frame.grid(sticky=tk.NSEW)
- right_frame.rowconfigure(0, weight=1)
- right_frame.columnconfigure(0, weight=1)
- right_frame.grid(sticky=tk.NSEW)
- pw.add(left_frame)
- pw.add(right_frame)
- pw.grid(sticky=tk.NSEW)
- canvas = tk.Canvas(right_frame, bg=style.lookup('TFrame', 'background'))
- canvas.frame = ttk.Frame(canvas)
- canvas.rowconfigure(0, weight=1)
- canvas.columnconfigure(0, weight=1)
- canvas.grid(sticky=tk.NSEW)
- canvas.frame.rowconfigure(990, weight=1)
- canvas.frame.columnconfigure(0, weight=1)
- canvas.frame.grid(sticky=tk.NSEW)
- content = ttk.Frame(canvas.frame)
- content.rowconfigure(0, weight=1)
- content.columnconfigure(0, weight=1)
- content.grid(sticky=tk.NSEW)
- xscroll = Scrollbar(right_frame, canvas, orient=tk.HORIZONTAL)
- yscroll = Scrollbar(right_frame, canvas, orient=tk.VERTICAL)
- xscroll.grid(row=990, column=0, sticky=tk.EW)
- yscroll.grid(row=0, column=990, sticky=tk.NS)
- for idx in range(1, 11):
- tk.Label(content, bg='#aaaaaa', fg='#000000', text=f'{idx} {lorem_ipsum}').grid(sticky=tk.NW)
- ttk.Separator(content, orient=tk.HORIZONTAL).grid(pady=10, sticky=tk.EW)
- for idx in range(11, 21):
- tk.Label(content, bg='#aaaaaa', fg='#000000', text=f'{idx} {lorem_ipsum}').grid(sticky=tk.NW)
- self.win = canvas.create_window((0, 0), window=canvas.frame, anchor=tk.NW)
- self.update_idletasks()
- pw.sashpos(0, newpos=100)
- def update_canvas(e):
- if e.width < content.bbox(tk.ALL)[2]:
- if not self.win:
- canvas.frame.grid()
- self.win = canvas.create_window((0, 0), window=canvas.frame, anchor=tk.NW)
- else:
- canvas.delete(self.win)
- canvas.frame.grid()
- self.win = None
- canvas.bind('<Configure>', update_canvas)
- canvas.configure(scrollregion=content.bbox(tk.ALL))
- canvas.configure(xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
- def main():
- app = App()
- app.mainloop()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement