#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
from tkinter import *
class Dialog(Toplevel):
'''Dialog class'''
def __init__(self, parent, title=None):
Toplevel.__init__(self, parent)
self.geometry('+{0:d}+{1:d}'.format(parent.winfo_rootx()+20,
parent.winfo_rooty()+20))
self.resizable(0, 0)
self.transient(parent)
if title:
self.title(title)
self.parent = parent
self.buttonbox()
body = Frame(self)
body.grid(column=0, row=0, padx=5, pady=5)
self.initial_focus = self.body(body)
if not self.initial_focus:
self.initial_focus = self
self.initial_focus.focus_set()
self.protocol('WM_DELETE_WINDOW', self.cancel)
self.grab_set()
self.wait_window(self)
def body(self, master):
pass
def button box(self):
box = Frame(self)
self.button1 = b1 = Button(box, text='OK', width=10,command=self.ok, default=ACTIVE)
self.button2 = b2 = Button(box, text='Cancel', width=10, command=self.cancel)
b1.pack(side=LEFT, padx=5, pady=5)
b2.pack(side=LEFT, padx=5, pady=5)
box.grid(column=0, row=1)
def ok(self, event=None):
if not self.validate():
self.initial_focus.focus_set()
return
self.withdraw()
self.update_idletasks()
self.apply()
self.cancel()
def cancel(self, event=None):
self.parent.focus_set()
self.destroy()
def validate(self):
return True
def apply(self):
pass
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.pack(expand=True, fill='both')
Button(self, text='Open', command=self.open_dialog).pack()
Button(self, text='Quit', command=master.destroy).pack()
self.createMenu(master)
def createMenu(self, master):
menubar = Menu(master)
master.config(menu=menubar)
file_menu = Menu(menubar, tearoff=False)
menubar.add_cascade(label='File', menu=file_menu)
file_menu.add_command(label='Open', under=0, command=self.open_dialog)
def open_dialog(self):
class My_Dialog(Dialog):
def body(self, master):
Label(master, text='Label').grid(column=0, row=0)
self.e = Entry(master, width=20)
self.e.grid(column=1, row=0)
return self.e
d = My_Dialog(self.master)
root = Tk()
app = Application(master=root)
app.mainloop()
|