Rawoas13

zoom function

Sep 10th, 2020
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.70 KB | None | 0 0
  1. from tkinter import *
  2.  
  3.  
  4. class Zoom:
  5.     def __init__(self):
  6.         self.zoom_scale = 1
  7.         self.zoom_count = 100
  8.         self.font_name, self.font_size = 'Helvetica', 10
  9.  
  10.         self.master = Tk()
  11.         self.master.title('ZOOM')
  12.  
  13.         self.text_widget_frame = Frame(self.master)
  14.         self.text_widget = Text(master=self.text_widget_frame, bd=0, undo=True, wrap='none')
  15.         self.vsb = Scrollbar(self.text_widget_frame, orient='vertical', command=self.text_widget.yview)
  16.         self.hsb = Scrollbar(self.text_widget_frame, orient='horizontal', command=self.text_widget.xview)
  17.         self.text_widget.configure(yscrollcommand=self.vsb.set, xscrollcommand=self.hsb.set)
  18.  
  19.         self.text_widget.grid(row=0, column=0, sticky='nsew')
  20.         self.vsb.grid(row=0, column=1, sticky='ns')
  21.         self.hsb.grid(row=1, column=0, sticky='ew')
  22.  
  23.         self.text_widget_frame.grid_rowconfigure(0, weight=1)
  24.         self.text_widget_frame.grid_columnconfigure(0, weight=1)
  25.         self.text_widget.focus_set()
  26.         self.text_widget_frame.pack(side='top', fill='both', expand=True)
  27.  
  28.         self.button_frame = Frame(self.master, bg='grey')
  29.         self.ZOOM_IN = Button(self.button_frame, text='ZOOM IN', command=self.zoom_in, bg='silver', fg='black', activebackground='silver', cursor='hand2')
  30.         self.ZOOM_OUT = Button(self.button_frame, text='ZOOM OUT', command=self.zoom_out, bg='silver', fg='black', activebackground='silver', cursor='hand2')
  31.         self.RESTORE_ZOOM = Button(self.button_frame, text='RESTORE ZOOM', command=self.restore_default_zoom, bg='silver', fg='black', activebackground='silver', cursor='hand2')
  32.         self.ZOOM_LABEL = Label(self.button_frame, text='100%', bg='red', fg='white')
  33.  
  34.         self.ZOOM_IN.pack(side=LEFT, padx=5)
  35.         self.ZOOM_OUT.pack(side=LEFT, padx=5)
  36.         self.RESTORE_ZOOM.pack(side=LEFT)
  37.         self.ZOOM_LABEL.pack(side=LEFT, padx=40)
  38.         self.button_frame.pack(side=BOTTOM)
  39.  
  40.         self.master.config(bg='grey')
  41.         self.master.mainloop()
  42.  
  43.     def zoom_in(self, event=None):
  44.         if self.zoom_count != 500:  # 500% is the maximum perecentage to zoom-in
  45.             self.zoom_scale += 1
  46.             font_size = self.font_size + self.zoom_scale
  47.  
  48.             self.text_widget.tag_add('zoom', '1.0', END)
  49.             self.text_widget.tag_config('zoom', font=(self.font_name, font_size))
  50.  
  51.             self.zoom_count += 10
  52.             self.ZOOM_LABEL['text'] = f'{self.zoom_count}%'
  53.  
  54.             if self.zoom_count == 100:  # Remove 'zoom' tag when self.zoom_count reaches 100
  55.                 self.restore_default_zoom()
  56.  
  57.     def zoom_out(self, event=None):
  58.         if self.zoom_count != 10:  # 10% is the minimum percentage to zoom-out
  59.             if 'zoom' in self.text_widget.tag_names():
  60.                 self.zoom_scale -= 1
  61.  
  62.             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
  63.                 self.zoom_scale -= 2
  64.  
  65.             font_size = self.font_size + self.zoom_scale
  66.  
  67.             if font_size != 0:
  68.                 self.text_widget.tag_add('zoom', '1.0', END)
  69.                 self.text_widget.tag_config('zoom', font=(self.font_name, font_size))
  70.  
  71.             self.zoom_count -= 10
  72.             self.ZOOM_LABEL['text'] = f'{self.zoom_count}%'
  73.  
  74.             if self.zoom_count == 100:  # Remove 'zoom' tag when self.zoom_count reaches 100
  75.                 self.restore_default_zoom()
  76.  
  77.     def restore_default_zoom(self, event=None):
  78.         self.zoom_scale = 1
  79.         self.zoom_count = 100
  80.         self.text_widget.tag_delete('zoom')
  81.         self.ZOOM_LABEL['text'] = f'{self.zoom_count}%'
  82.  
  83.  
  84. if __name__ == '__main__':
  85.     Zoom()
  86.  
Add Comment
Please, Sign In to add comment