here2share

# Tk_basic_skeleton.py

Sep 12th, 2020
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.64 KB | None | 0 0
  1. # Tk_basic_skeleton.py (found online)
  2.  
  3. import sys
  4. import Tkinter as tk
  5. from tkFileDialog import askopenfilename, asksaveasfilename
  6.  
  7.  
  8. class App(tk.Tk):
  9.  
  10.     """
  11.    A simple demo of the basic Tkinter widgets. This can be used as a skeleton for a simple GUI applications.
  12.    """
  13.  
  14.     def __init__(self):
  15.         tk.Tk.__init__(self)
  16.  
  17.  
  18.         self.create_widget_frame()
  19.  
  20.         button_box = tk.Frame(self)
  21.         tk.Button(button_box, text='OK', command=self.on_ok_clicked).grid(pady=15)
  22.         button_box.pack()
  23.  
  24.         self.create_menu()
  25.         self.set_keybindings()
  26.         App.center_on_screen(self)
  27.  
  28.  
  29.     @staticmethod
  30.     def center_on_screen(toplevel):
  31.         toplevel.update_idletasks()
  32.         w = toplevel.winfo_screenwidth()
  33.         h = toplevel.winfo_screenheight()
  34.         size = tuple(int(_) for _ in toplevel.geometry().split('+')[0].split('x'))
  35.         x = w/2 - size[0]/2
  36.         y = h/2 - size[1]/2
  37.         toplevel.geometry('%dx%d+%d+%d' % (size + (x, y)))
  38.  
  39.  
  40.     def create_widget_frame(self):
  41.  
  42.         label_config = {'sticky': tk.E, 'column': 0, 'padx': 5, 'pady': 5}
  43.         widget_config = {'sticky': tk.W, 'column': 1, 'padx': 5, 'pady': 5}
  44.  
  45.         widget_frame = tk.Frame(self)
  46.         current_row = 0
  47.  
  48.         tk.Label(widget_frame, text='Entry (text): ').grid(label_config, row=current_row)
  49.         self.entry = tk.Entry(widget_frame)
  50.         self.entry.grid(row=current_row, column=1, sticky=tk.E)
  51.         current_row += 1
  52.  
  53.         tk.Label(widget_frame, text='Scale (number): ').grid(label_config, row=current_row)
  54.         self.scale = tk.Scale(widget_frame, from_=0, to=10, resolution=0.1, orient=tk.HORIZONTAL)
  55.         self.scale.grid(widget_config, row=current_row)
  56.         current_row += 1
  57.  
  58.         tk.Label(widget_frame, text='Checkbox (boolean): ').grid(label_config, row=current_row)
  59.         self.checkbox_val = tk.IntVar()
  60.         self.checkbox = tk.Checkbutton(widget_frame, variable=self.checkbox_val)
  61.         self.checkbox.grid(widget_config, row=current_row)
  62.         current_row += 1
  63.  
  64.         tk.Label(widget_frame, text='Spinbox (number): ').grid(label_config, row=current_row)
  65.         self.spinbox = tk.Spinbox(widget_frame, from_=0, to=10)
  66.         self.spinbox.grid(widget_config, row=current_row)
  67.         current_row += 1
  68.  
  69.         self.enum_val = tk.StringVar(widget_frame)
  70.         self.enum_val.set('one') # default value
  71.         tk.Label(widget_frame, text='OptionMenu (enum): ').grid(label_config, row=current_row)
  72.         self.combobox = tk.OptionMenu(widget_frame, self.enum_val, 'one', 'two', 'three')
  73.         self.combobox.grid(widget_config, row=current_row)
  74.         current_row += 1
  75.  
  76.         widget_frame.pack()
  77.  
  78.  
  79.     def set_keybindings(self):
  80.         self.bind_all('<Control-o>', lambda event: self.open_file())
  81.         self.bind_all('<Control-s>', lambda event: self.save_file())
  82.         self.bind_all('<Control-q>', self.quit_app)
  83.         self.bind_all('<Control-h>', lambda event: self.show_help())
  84.         self.bind_all('<Return>', lambda event: self.on_ok_clicked())
  85.  
  86.  
  87.     def on_ok_clicked(self):
  88.         print 'Entry text: %s' % self.entry.get()
  89.         print 'Scale value: %.1f' % self.scale.get()
  90.         print 'Checkbutton value: %i' % self.checkbox_val.get()
  91.         print 'Spinbox value: %i' % int(self.spinbox.get())
  92.         print 'OptionMenu value: %s' % self.enum_val.get()
  93.  
  94.  
  95.     def create_menu(self):
  96.         menubar = tk.Menu(self)
  97.        
  98.         fileMenu = tk.Menu(menubar, tearoff=False)
  99.         menubar.add_cascade(label="File", underline=0, menu=fileMenu)
  100.         fileMenu.add_command(label="Open", underline=1, command=self.open_file, accelerator="Ctrl+O")
  101.         fileMenu.add_command(label="Save", underline=1, command=self.save_file, accelerator="Ctrl+S")
  102.         fileMenu.add_command(label="Quit", underline=1, command=self.quit_app, accelerator="Ctrl+Q")
  103.        
  104.         helpMenu = tk.Menu(menubar, tearoff=False)
  105.         menubar.add_cascade(label="Help", underline=0, menu=helpMenu)
  106.         helpMenu.add_command(label="Help", underline=1, command=self.show_help, accelerator="Ctrl+H")
  107.         helpMenu.add_command(label="About", underline=1, command=self.about_app)
  108.        
  109.         self.config(menu=menubar)
  110.  
  111.  
  112.     def open_file(self):
  113.         """Options are explained here: http://tkinter.unpythonic.net/wiki/tkFileDialog"""
  114.         filename = askopenfilename(title='Open a file')
  115.         if filename:
  116.             print 'Open and do something with %s' % filename
  117.  
  118.  
  119.     def save_file(self):
  120.         """Options are explained here: http://tkinter.unpythonic.net/wiki/tkFileDialog"""
  121.         filename = asksaveasfilename()
  122.         if filename:
  123.             print 'Save something to %s' % filename
  124.  
  125.  
  126.     def quit_app(self, event):
  127.         sys.exit(0)
  128.  
  129.  
  130.     def show_help(self):
  131.         print 'Open a link to online help/wiki here'
  132.  
  133.  
  134.     def about_app(self):
  135.         # FIXME: pressing return correctly closes dialog, but also incorrectly fires the main window's 'on_click' method
  136.         about_text = """
  137.        Put info about the application here, e.g., name, author(s), version,
  138.        license, etc. This text will appear in the \"about\" dialog."""
  139.         about_dialog = tk.Toplevel(self)
  140.         about_dialog.title('About App')
  141.         about_dialog.bind('<Escape>', lambda event: about_dialog.destroy())
  142.         about_dialog.bind('<Return>', lambda event: about_dialog.destroy())
  143.         App.center_on_screen(about_dialog)
  144.         tk.Message(about_dialog, text=about_text).pack()
  145.         button = tk.Button(about_dialog, text='Close', command=about_dialog.destroy).pack()
  146.  
  147. if __name__ == "__main__":
  148.     app = App()
  149.     app.mainloop()
Add Comment
Please, Sign In to add comment