Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- class Zoom:
- def __init__(self):
- self.zoom_scale = 1
- self.zoom_count = 100
- self.font_name, self.font_size = 'Helvetica', 10
- self.master = Tk()
- self.master.title('ZOOM')
- self.text_widget_frame = Frame(self.master)
- self.text_widget = Text(master=self.text_widget_frame, bd=0, undo=True, wrap='none')
- self.vsb = Scrollbar(self.text_widget_frame, orient='vertical', command=self.text_widget.yview)
- self.hsb = Scrollbar(self.text_widget_frame, orient='horizontal', command=self.text_widget.xview)
- self.text_widget.configure(yscrollcommand=self.vsb.set, xscrollcommand=self.hsb.set)
- self.text_widget.grid(row=0, column=0, sticky='nsew')
- self.vsb.grid(row=0, column=1, sticky='ns')
- self.hsb.grid(row=1, column=0, sticky='ew')
- self.text_widget_frame.grid_rowconfigure(0, weight=1)
- self.text_widget_frame.grid_columnconfigure(0, weight=1)
- self.text_widget.focus_set()
- self.text_widget_frame.pack(side='top', fill='both', expand=True)
- self.button_frame = Frame(self.master, bg='grey')
- self.ZOOM_IN = Button(self.button_frame, text='ZOOM IN', command=self.zoom_in, bg='silver', fg='black', activebackground='silver', cursor='hand2')
- self.ZOOM_OUT = Button(self.button_frame, text='ZOOM OUT', command=self.zoom_out, bg='silver', fg='black', activebackground='silver', cursor='hand2')
- self.RESTORE_ZOOM = Button(self.button_frame, text='RESTORE ZOOM', command=self.restore_default_zoom, bg='silver', fg='black', activebackground='silver', cursor='hand2')
- self.ZOOM_LABEL = Label(self.button_frame, text='100%', bg='red', fg='white')
- self.ZOOM_IN.pack(side=LEFT, padx=5)
- self.ZOOM_OUT.pack(side=LEFT, padx=5)
- self.RESTORE_ZOOM.pack(side=LEFT)
- self.ZOOM_LABEL.pack(side=LEFT, padx=40)
- self.button_frame.pack(side=BOTTOM)
- self.master.config(bg='grey')
- self.master.mainloop()
- def zoom_in(self, event=None):
- if self.zoom_count != 500: # 500% is the maximum perecentage to zoom-in
- self.zoom_scale += 1
- font_size = self.font_size + self.zoom_scale
- self.text_widget.tag_add('zoom', '1.0', END)
- self.text_widget.tag_config('zoom', font=(self.font_name, font_size))
- self.zoom_count += 10
- self.ZOOM_LABEL['text'] = f'{self.zoom_count}%'
- if self.zoom_count == 100: # Remove 'zoom' tag when self.zoom_count reaches 100
- self.restore_default_zoom()
- def zoom_out(self, event=None):
- if self.zoom_count != 10: # 10% is the minimum percentage to zoom-out
- if 'zoom' in self.text_widget.tag_names():
- self.zoom_scale -= 1
- else: # This is to avoid font_size to become 0 when user tries to zoom out when there is no 'zoom' tag in text_widget
- self.zoom_scale -= 2
- font_size = self.font_size + self.zoom_scale
- if font_size != 0:
- self.text_widget.tag_add('zoom', '1.0', END)
- self.text_widget.tag_config('zoom', font=(self.font_name, font_size))
- self.zoom_count -= 10
- self.ZOOM_LABEL['text'] = f'{self.zoom_count}%'
- if self.zoom_count == 100: # Remove 'zoom' tag when self.zoom_count reaches 100
- self.restore_default_zoom()
- def restore_default_zoom(self, event=None):
- self.zoom_scale = 1
- self.zoom_count = 100
- self.text_widget.tag_delete('zoom')
- self.ZOOM_LABEL['text'] = f'{self.zoom_count}%'
- if __name__ == '__main__':
- Zoom()
Add Comment
Please, Sign In to add comment